Get Job - PowerShell


Overview

Get-Job is a PowerShell cmdlet that retrieves information about running or completed jobs in the current session. It provides a convenient way to monitor and manage jobs, track their status, and retrieve their results.

Syntax

Get-Job [-Id] <JobId> [-JobState <JobState>] [-State <JobState>] [-Name <String>] [[-FilterScript] <ScriptBlock>] [-ErrorAction <ErrorAction>] [-PassThru] [-Token <String>] [-ThrottleLimit <Int32>] [-Wait]

Options/Flags

  • -Id: Specifies the ID of the job you want to retrieve information about.
  • -JobState: Specifies the job state you want to filter by (e.g., Running, Completed, Suspended).
  • -State: Alias for -JobState.
  • -Name: Filters jobs by their name.
  • -FilterScript: Specifies a script block to filter jobs based on custom conditions.
  • -ErrorAction: Controls the behavior when an error occurs.
  • -PassThru: Returns the job objects instead of just their state.
  • -Token: Specifies the token used to communicate with the remote session where the job is running.
  • -ThrottleLimit: Controls the maximum number of operations that can be performed per second.
  • -Wait: Suspends execution until the specified job completes.

Examples

Simple usage:

Get-Job -Id 123

Filter by job state:

Get-Job -JobState Running

Use a filter script:

Get-Job -FilterScript { $_.Name -like "MyJob*" }

Retrieve job results:

Get-Job -Id 123 -PassThru | Receive-Job

Common Issues

  • Unable to retrieve job information: Ensure the job is still running or has finished successfully. If not, use Get-Job -JobState Completed to retrieve completed jobs.
  • Job not found: Verify the specified job ID or name is correct.

Integration

  • Use Start-Job to start a new job and then Get-Job to monitor its progress and retrieve its results.
  • Combine with Receive-Job to receive the job’s output and handle exceptions.
  • Utilize Invoke-Command -AsJob to run commands in a remote session and then Get-Job to retrieve their status and results.