Code Reference

Jobs

PowerShell · Reference cheat sheet

Jobs

PowerShell · Reference cheat sheet


📋 Overview

Jobs run work in the background. Classic jobs (Start-Job) use separate processes; thread jobs (Start-ThreadJob) are lighter. PowerShell 7 also offers ForEach-Object -Parallel. Use jobs for fan-out, long tasks, and remoting aggregation.

🔧 Core concepts

CmdletRole
Start-JobBackground process job
Start-ThreadJobSame-process thread (module)
Get-JobList
Wait-JobBlock until done
Receive-JobCollect output
Remove-JobCleanup
Stop-JobCancel
Invoke-Command -AsJobRemote as job

Child jobs appear under parent job objects for remoting fan-out. Always Receive-Job + Remove-Job to avoid leaks.

💡 Examples

Local jobs:

$j = Start-Job -ScriptBlock {
  Start-Sleep -Seconds 2
  Get-Date
}
Wait-Job $j | Out-Null
Receive-Job $j
Remove-Job $j

ThreadJob:

Install-Module ThreadJob -Scope CurrentUser
$jobs = 1..5 | ForEach-Object {
  Start-ThreadJob { param($n) Start-Sleep 1; $n * 2 } -ArgumentList $_
}
$jobs | Wait-Job | Receive-Job
$jobs | Remove-Job

Parallel foreach (PS 7+):

1..10 | ForEach-Object -Parallel {
  Invoke-RestMethod "https://httpbin.org/get?n=$_"
} -ThrottleLimit 4

⚠️ Pitfalls

  • Jobs don’t share live variables with the parent—pass -ArgumentList or $using: (remote/parallel).
  • Forgetting Receive-Job leaves output buffered and jobs in Completed state forever.
  • Start-Job is heavy (new runspace/process)—prefer ThreadJob/parallel for many small tasks.
  • Errors surface when receiving—check job State and ChildJobs.
  • UI/host-interactive cmdlets fail inside jobs.
  • Remoting jobs need sessions cleaned up (Remove-PSSession).

On this page