PowerShell Background Jobs

suggest change

Introduction

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:

  1. The job runs in a clean process, including environment.
  2. The job can run asynchronously to your main PowerShell process

Cons:

  1. Process environment changes will not be present in the job.
  2. 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`).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents