PowerShell 2.0: One Cmdlet at a Time 15 Start-Job
Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Start-Job cmdlet.
What can I do with it?
Start a background job on the local computer. This allows you to take back your console session whilst you wait for the job to complete.
Examples:
Start a background job to run Get-Service on the local computer.
Start-Job -ScriptBlock {Get-Service}
This will display the status of this job in your current session and allow you to continue working in the session - then retrieve the results at a later time.
You could also start a background job with a script, not just a scriptblock or a command.
Start-Job -FilePath .\Test.ps1
To start a background job on a remote computer use the -AsJob parameter available on a number of cmdlets.
(Tip: to find out which cmdlets have the -AsJob parameter use Get-Command to give you a list
Get-Command | Where-Object {$_.definition -match ‘asjob’}
)
So to start a job to find services on the remote computer Server1
Get-WmiObject Win32_Service -ComputerName Server1 -AsJob
How could I have done this in PowerShell 1.0?
The concept of jobs did not exist in PowerShell 1.0. You would have needed to open an extra PowerShell session whilst you waited for a command to complete in your current session.