PowerShell is one of my favorite tool when it comes to automation. It has lot of functionalities and one of its cool features is Start-Job
. This will let you run tasks in the background while you keep using PowerShell for other things.
Start-Job is ideal to use when …
- Tasks take a long time to complete
- You want to avoid module conflicts by isolating them in separate jobs
- You need to resolve assembly conflicts by running jobs in isolated processes
How to Use Start-Job
I will try to explain this with a simple example below.
Example : Displaying the Date and Time using Get-Date
Below examples shows how to run a background job in PowerShell to get the current date and time. It uses Start-Job
to begin the job, waits for it with Wait-Job
, gets the result using Receive-Job
, and cleans up with Remove-Job
.
# Start a background job to display the current date and time
$job = Start-Job -ScriptBlock {
Get-Date
}
# Check if the job is complete
Write-Output "Job ID: $($job.Id) started."
Wait-Job -Job $job
# Get the result of the job
$jobResult = Receive-Job -Job $job
Write-Output "The current date and time is: $jobResult"
# Clean up
Remove-Job -Job $job
Output
It’s quite simple, isn’t it? Just make sure to use Remove-Job
to clean up after your job is done. If you skip this step, it can lead to duplication or conflicts with other jobs.
Why I recommend to use Jobs?
Numerous reasons but, the major reason is that some PowerShell modules can conflict when loaded together in the same session. For example, Microsoft Graph and PnP PowerShell might have overlapping dependencies. Running these modules or assemblies in separate jobs make sures that they operate independently without issues.
Try it out and let me know how you use it in your own scripts!