PowerShell Background Jobs
suggest changeIntroduction
Jobs were introduced in PowerShell 2.0 and helped to solve a problem inherent in the command-line tools. In a nutshell, if you start a long running task, your prompt is unavailable until the task finishes. As an example of a long running task, think of this simple PowerShell command:
Get-ChildItem -Path c:\ -Recurse
It will take a while to fetch the full directory list of your C: drive. If you run it as a job, the console is available again and you can capture the result later.
Remarks
Start-Job runs work in a new process. This has related pros and cons. Thread jobs (Start-ThreadJob, PowerShell 6 and later) run in another runspace in the same process, so the process-isolation points below do not apply to them.
Pros:
- The job runs in a clean process, including environment.
- The job can run asynchronously to your main PowerShell process
Cons:
- Process environment changes will not be present in the job.
- Parameters passed in, and results returned, are serialized.
- This means if you change a parameter object while the job is running it will not be reflected in the job.
- This also means if an object cannot be serialized you cannot pass or return it (although PowerShell may copy any parameters and pass or return a `PSObject`).
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents