PowerShell PowerShell Background Jobs

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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 full directory list of your C: drive. If you run it as Job then the console will get the control back and you can capture the result later on.

Remarks

PowerShell Jobs run in a new process. This has pros and cons which are related.

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 pass to and returned results 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/return a PSObject.)


Got any PowerShell Question?