liveBook · Manning

Background processing

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

A job is a PowerShell-managed background operation that can run asynchronously and be monitored or controlled separately from the interactive pipeline.

Background processing lets PowerShell run a command asynchronously while you continue using the shell. PowerShell calls these background commands jobs, which can be monitored and controlled separately from the interactive pipeline. section 14.1 section 14.2

Run a command synchronously while testing it so prompts, errors, and results are visible. Move it to the background only after it works reliably; background jobs cannot receive interactive input or prompt for missing parameters. section 14.2

Manage a job through a short monitoring-and-results workflow:

  • Start work with Start-Job, Start-ThreadJob, or Invoke-Command using -AsJob. section 14.11 section 14.5
  • Check status and metadata with Get-Job, optionally selecting a job by ID or name. section 14.7
  • Retrieve cached output with Receive-Job; use -Keep to retain it for another retrieval or pipe it to tools such as Sort-Object and Export-CSV. section 14.7 section 14.12
  • Stop unwanted work with Stop-Job, either from Get-Job or by specifying a job name. section 6.7 section 6.8

Search files without blocking the shell

Start-ThreadJob { gci / -recurse -filter '*.txt' } Get-Job Receive-Job -id 1 -Keep The first command searches for text files in a one-time thread job. Get-Job shows whether it has completed, and Receive-Job retrieves its results while preserving a copy. section 14.11 section 14.12

PS C:\> invoke-command -command { get-process } 
-hostname (get-content .\allservers.txt ) 
-asjob -jobname MyRemoteJob
WARNING: column "Command" does not fit into the display and was removed.
Id              Name            State      HasMoreData     Location
--              ----            -----      -----------     --------
8               MyRemoteJob     Running    True            server-r2,lo...

Inspect a named remote job created with Invoke-Command -AsJob, including its running state, location, and parallel target computers. section 14.5

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains asynchronous behavior, job creation, status checks, result retrieval, retention, stopping, remoting, and session lifetime.
Examples
Uses filesystem searches, remote process queries, job listings, Receive-Job output, and exercises involving groups of computers.
What it assumes
Assumes basic PowerShell commands and remoting, while introducing synchronous-versus-asynchronous execution and job terminology.
What you can do after
Operate PowerShell jobs by starting, monitoring, retrieving, retaining, processing, and stopping asynchronous results.
How it connects
Connects jobs with synchronous commands, asynchronous execution, thread jobs, remoting, child jobs, and deserialized objects.

Also explained in

Compare how 4 books explain background processing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.