Search Tools Links Login

Powershell Background Jobs

Posted: 2022-12-20
By: dwirch
Viewed: 85

Filed Under:

PowerShell Code Cache, Powershell, Blog, Scripting, Tip

No attachments for this post


In PowerShell, you can use background jobs to run scripts or commands asynchronously, meaning they can run in the background while you continue to work in the console. This can be useful if you have a long-running task that you don't want to wait for, or if you want to run multiple tasks concurrently.

To create a background job in PowerShell, you can use the Start-Job cmdlet. For example:

Start-Job -ScriptBlock { Get-Process }

This will create a background job that runs the Get-Process cmdlet. To view the job, you can use the Get-Job cmdlet:

Get-Job

This will display information about the job, including its status, ID, and name.

To retrieve the results of the job, you can use the Receive-Job cmdlet:

Receive-Job -Id 1

This will display the results of the job with the ID of 1.

You can also use the Wait-Job cmdlet to wait for a background job to complete before continuing with your script:

Wait-Job -Id 1

Finally, you can use the Remove-Job cmdlet to remove a background job from the job queue:

Remove-Job -Id 1

Overall, background jobs can be a useful tool for running long-running tasks or multiple tasks concurrently in PowerShell. By using the cmdlets discussed above, you can create, view, retrieve the results of, and remove background jobs in your scripts.


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.