PowerShell Jobs
Background jobs with Start-Job, Receive-Job, and parallel execution workflows.
Background Jobs Quick Reference
Start a background job
$job = Start-Job -ScriptBlock { Get-EventLog System -Newest 1000 }
Check job status
Get-Job
Get-Job -Id 1 | Select-Object Id, Name, State, HasMoreData
Get job results
Receive-Job -Id 1
Receive-Job -Id 1 -Keep # Keep results for re-reading
Wait for job to complete
Wait-Job -Id 1
$result = Receive-Job -Id 1
Run on multiple servers in parallel
$servers = "server01", "server02", "server03"
$jobs = $servers | ForEach-Object {
Start-Job -ScriptBlock { param($s) Test-Connection $s -Count 4 } -ArgumentList $_
}
$jobs | Wait-Job | Receive-Job
Cleanup
Get-Job -State Completed | Remove-Job
Get-Job | Stop-Job | Remove-Job # Kill and clean all
ForEach-Object -Parallel (PowerShell 7+)
$servers | ForEach-Object -Parallel {
Test-Connection $_ -Count 1
} -ThrottleLimit 5