PowerShell Get-Job

Display PowerShell's Background JobsPowerShell Get-Job Cmdlet

The purpose of Get-Job is to list both the PowerShell jobs running in the background, and those that have completed.

Windows PowerShell Get-Job Topics

 ♣

 

Introduction to the Get-Job Cmdlet

This example of a PowerShell 'Get-xyz' verb is unusual in that you need to create some jobs with Start-Job before Get-Job returns any objects.

Another key concept is Get-Job cannot display the details of the results contained in the job.  To see the actual data you need a third cmdlet called Receive-Job.

Example 1: List Running Services in a Job

a) Preamble: Create Jobs with Start-Job
We need to create a few jobs with Start-Job; for testing Windows Processes or Services would provide a suitable vehicle for creating a background job.

Start-Job -ScriptBlock {Get-Service |
Where-Object {$_.Status -eq "Running"}
}

Note 1: This is a -ScriptBlock example rather than a case for the -FilePath parameter.  Where I introduced a Where-Object clause, a real PowerShell job would probably be more complex and time-consuming.

b) Get-Job lists Jobs
Here is an example of our featured cmdlet in action.

Clear-host
Get-Job | Format-Table Id, Name, State, HasMoreData, Command -Auto

Id Name State HasMoreData          Command
—  —-    —–    ————              —————————
2 Job2 Completed True Get-Service | Where {$_.Status -eq "Running"}
4 Job4 Completed True Get-Service | …

c) Call for Receive-Job
Remember Get-Job does not show the running services contained in the underlying job, for that we need the sister cmdlet: Receive-Job.

Get-Job -Name Job2 | Receive-Job -Keep

Note 2: When testing with Receive-Job, the -Keep parameter prevents the results from evaporating after the first run.

Note 3: I confess: Get-Job is not essential in the above script, I just used it to illustrate a techique; this command works just as well:
Receive-Job -ID 2 -Keep.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Filtering with Get-Job

PowerShell's Where-Object is handy to filter the jobs.  One use is to check that 'HasMoreData' is true before you pipe the output into Receive-Job.

Clear-host
Get-Job | Where-Object {$_.HasMoreData -eq "True"} |
Format-Table Id, Name, State, HasMoreData, Command -AutoSize

Note 4: You could also filter on 'State' to check which jobs have completed.

Researching Get-Job

The benefit of creating 'Jobs' is that time-consuming tasks can continue in the background while you execute other commands in parallel.

# Research PowerShell Background Job
Clear-Host
Get-Help Get-Job -Full

Note 5: Observe the variety ways of calling for the jobs:

  • Get-Job [-ID]
  • Get-Job [-Name]
  • Get-Job [-State]
  • Get-Job [-Filter]

Example 2: Start-Job | Format-List

It is interesting that any PowerShell jobs created by Start-Job generate both a parent and a child job.  Normally, it's the child job that does the actual work. If you employ Invoke-Command with its –AsJob parameter against remote computers, you see one child job per remote computer.

The purpose of this example is to learn more about childjobs, and explain the odd and even numbered jobs.

Clear-Host
Remove-Job -Name *
Start-Job -ScriptBlock{Get-Process} -Name GuyProc
Start-Job -ScriptBlock{Get-Service} -Name GuyServ
Get-Job | Format-List Id, Name, ChildJobs, State

Note 6: Remove-Job -Name * deletes all jobs.  Your Id numbers may differ from mine, it depends on how much you have been experimenting.

Id : 2
Name : GuyProc
ChildJobs : {Job3}
State : Running

Id : 4
Name : GuyServ
ChildJobs : {Job5}
State : Running

Note 7: When you manage jobs, anything you do to the parent job is automatically applied to the child jobs.  For example, stopping the parent job also stops any child jobs.  Fortunately, getting the results of the parent job also returns the results of the child jobs.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Researching Similar PowerShell Cmdlets

# PowerShell Job Cmdlet Research
Get-Command -Verb Job

Name
——————–
Get-Job
Invoke-Command
Receive-Job
Remove-Job
Resume-Job
Start-Job
Stop-Job
Suspend-Job
Wait-Job
about_Job_Details
about_Remote_Jobs
about_Jobs

See more PowerShell Techniques »

Summary of PowerShell Get-Job

The purpose of this cmdlet is to list the background jobs, see which have completed, and which have data that can be viewed with Receive-Job.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Home   • Real life tasks   • Invoke-Command   • PowerShell Windows 7

Remote PowerShell   • PowerShell WSMan  • -Online   • PowerShell WinRm   • Test-Connection

Jobs  -AsJob   • Receive-Job   • Get-Job   • Receive-Job   • Free WMI Monitor

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.