Powershell

How to Use the Start-Job (Microsoft.PowerShell.Core) Cmdlet in PowerShell?

PowerShell can run the jobs in the background without even getting noticed or interacting with the current session. For such a purpose, the “Start-Job” cmdlet is used in PowerShell. It can start custom-type job instances. Moreover, the user can continue to work on other tasks in the current session without any interruption while the job continues to run.

This post will elaborate on the cmdlet “Start-Job” in PowerShell.

What is the “Start-Job” Cmdlet in PowerShell?

The “Start-Job” cmdlet starts the job in the background on a local computer’s current session. It returns the job object immediately, despite the fact that the job is taking longer than expected time to finish it. Job objects contain useful information regarding the job.

How Does the “Start-Job” Cmdlet Work?

As it is described above, the “Start-Job” cmdlet is used for starting a job in the background of a local computer. Let’s have a look at practical demonstrations of the given cmdlet.

Example 1: Use the “Start-Job” Cmdlet to Start a Background Job in PowerShell

To start a job in the background, the “Start-Job” cmdlet can be used. Then, add the “-ScriptBlock” parameter and assign it the stated command to start a job:

Start-Job -ScriptBlock {Get-Process -Name system}

Example 2: Use the “Start-Job” Cmdlet to Run the Script as a Background Job

To run a script as a background job, first, specify the “Start-Job” cmdlet and then provide the script file path by using the “-Path” parameter:

Start-Job -FilePath C:\Docs\Script.ps1

Example 3: Use the “Start-Job” Cmdlet to Get a Process Using a Background Job

Execute the following command to get a process by using its name:

Start-Job -Name explorer -ScriptBlock {Get-Process -Name PowerShell}

According to the above-stated command:

  • Initially, add the “Start-Job” cmdlet alongside the “-Name” parameter and assign it the process name.
  • Next, specify the “-ScriptBlock” parameter and assign it the stated cmdlet to initiate a job:

Example 4: Use the “Start-Job” Cmdlet With the “-ArgumentList” Parameter to Specify an Array

To specify an argument array list, execute the provided command:

Start-Job -ScriptBlock {Get-Process -Name $New} -ArgumentList pwsh, notepad

Here:

  • Use the “Start-Job” cmdlet and the “-ScriptBlock” parameter having the stated cmdlet assigned to it.
  • Next, mention the “-ArgumentList” parameter and provide it with two arguments separated by a comma:

That’s all! We have demonstrated PowerShell’s “Start-Job” cmdlet in-depth.

Conclusion

The “Start-Job” cmdlet in PowerShell is used to start a background process on a local computer. Users can continue to work on other tasks while the background job continues to run. This post has explained the “Start-Job” cmdlet in detail with the assistance of several examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.