🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Background Jobs

A background job is work that runs on its own while you keep using the shell. It lets a slow task run without blocking your next command.

Start a job and collect its result

Start-Job runs a script block in the background. Wait for it, then collect its result.

$job = Start-Job -ScriptBlock { "backup done" }
Wait-Job $job | Out-Null
Receive-Job $job
Remove-Job $job

Read the job state after waiting

After Wait-Job finishes, the State property reads Completed. Collect the result and clean up.

$job = Start-Job -ScriptBlock { "task done" }
Wait-Job $job | Out-Null
$job.State
Receive-Job $job | Out-Null
Remove-Job $job

One job can return several lines

A script block can output more than one value. Receive-Job prints each value on its own line.

$job = Start-Job -ScriptBlock { "alpha"; "beta" }
Wait-Job $job | Out-Null
Receive-Job $job
Remove-Job $job

Example

$job = Start-Job -ScriptBlock { "report ready" }
Wait-Job $job | Out-Null
$job.State
Receive-Job $job
Remove-Job $job

Key points

  • Background job: work that runs on its own while the shell stays free for other commands.
  • Start-Job: starts a background job from a script block.
  • Wait-Job: waits until a job finishes before the script moves on.
  • Receive-Job: collects the results a finished job produced.
  • Job state: the State property tells whether a job is running or completed.

In this example, Start-Job starts a background job, Wait-Job waits for it to finish, the State property prints Completed, Receive-Job prints the result, and Remove-Job cleans the job up.

Note

a finished job stays around until Remove-Job deletes it, so clean up each job after collecting its results.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which command waits until a background job finishes?