Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Background Jobs

 

Cmdlets can perform their action internally or as a Windows PowerShell background job. When a cmdlet runs as a background job, the work is done asynchronously in its own thread separate from the pipeline thread that the cmdlet is using. From the user perspective, when a cmdlet runs as a background job, the command prompt returns immediately even if the job takes an extended amount of time to complete, and the user can continue without interruption while the job runs.

The job object that is returned by the cmdlets that support background jobs defines the job. (The Start-Job cmdlet also returns a job object.) The name of the job, an identifier that is used to specify the job, the state information, and the child jobs are included in this definition. The job does not perform any of the work. Each background job has at least one child job because the child job performs the actual work. When you run a cmdlet so that the work is performed as a background job, the cmdlet must add the job and the child jobs to a common repository, referred to as the job repository.

For more information about how background jobs are handled at the command line, see the following:

To write a cmdlet that can be run as a background job, you must complete the following tasks:

  • Define an asJob switch parameter so that the user can decide whether to run the cmdlet as a background job.

  • Create an object that derives from the Job class. This object can be a custom job object or a job object provided by Windows PowerShell, such as a PSEventJob object.

  • In a record processing method, add an if statement that detects whether the cmdlet should run as a background job.

  • For custom job objects, implement the job class.

  • Return the appropriate objects, depending on whether the cmdlet is run as a background job.

For a code example, see How to Support Jobs.

The following APIs are provided by Windows PowerShell to manage background jobs.

Job

Derives custom job objects. This is an abstract class.

JobRepository

Manages and provides information about the current active background jobs.

JobState

Defines the state of the background job. States include Started, Running, and Stopped.

JobStateInfo

Provides information about the state of a background job and, if the last state change was caused by an error, the reason the job entered its current state.

JobStateEventArgs

Provides the arguments for an event that is raised when a background job changes state.

The following cmdlets are provided by Windows PowerShell to manage background jobs.

Get-Job

Gets Windows PowerShell background jobs that are running in the current session.

Receive-Job

Gets the results of the Windows PowerShell background jobs in the current session.

Remove-Job

Deletes a Windows PowerShell background job.

Start-Job

Starts a Windows PowerShell background job.

Stop-Job

Stops a Windows PowerShell background job.

Wait-Job

Suppresses the command prompt until one or all of the Windows PowerShell background jobs running in the session are complete.

Show: