I'm a little confused about the difference between Start-Job
and Start-Process
in PowerShell. I know that Start-Job
will run in the background, but I'm wondering whether things run differently with Start-Job
than with Start-Process
and whether there are other implications of using one as opposed to the other . When should you use one over the other, and are there advantages that either has over the other?
The Start-Job cmdlet starts a PowerShell background job on the local computer. A PowerShell background job runs a command without interacting with the current session. When you start a background job, a job object returns immediately, even if the job takes an extended time to finish.
The following command starts a job object and saves the resulting job object in the $job variable. Beginning in PowerShell 6.0, you can use the background operator ( & ) at the end of a pipeline to start a background job.
Automatic Jobs with -AsJob When the job completes, get the result of the job with Receive-Job . Receive-Job returns the result from the cmdlet as if the -AsJob flag were not present. For example, the Receive-Job result of Do-Action -AsJob is of the same type as the result of Do-Action . Azure PowerShell Copy.
To get all jobs and their status use the Get-Job PowerShell cmdlet. The command gets the jobs in the current PowerShell session. The output includes a background job, a remote job and several instances of a scheduled job.
Start-Job starts a background job and creates a job object that you use to monitor, query, and interact with the job using the cmdlets Get-Job, Receive-Job, Wait-Job, Stop-Job, and Remove-Job. You won't see any interactive windows or console output until you query the job object with Receive-Job. That's what "background job" means - that it runs, but doesn't interact with the logon session. However, if there's any output, that's collected by the job object, and you can retrieve it with Receive-Job. You can generally tell if there's data to receive by checking the HasMoreData property of the job object, but be careful, that's buggy in PowerShell 2 - remember this? "HasMoreData" is true even after Receive-Job
Start-Process launches a process that runs interactively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With