How to run PowerShell commands in Background?

PowerShellMicrosoft TechnologiesSoftware & Coding

To run commands in the background in the PowerShell, you need to use Background job cmdlets. Background job means running commands/job in the background without occupying the console.

Start-Job is one of the job scheduler cmdlets for PowerShell which runs PowerShell commands in the background without interacting with the current user session as a Job so that users can work in the PowerShell console without losing the control of the console while the command is running in the background.

When PowerShell's job starts using Start-Job, a job returns the object immediately even if the job takes an extended time.

Start-Job is designed to run on the local computer because it doesn’t have the remote job parameter. To run the job remotely you need to use Invoke-Command with -AsJob parameter.

Example

Start-Job -ScriptBlock{Get-EventLog -LogName System}

Once you run the command, the below output will be generated immediately.

Output

Id    Name    PSJobTypeName    State    HasMoreData    Location
--    ----    -------------    -----    -----------    --------
1    Job1    BackgroundJob    Running       True      localhost

If you check the above output, the job state is running. More commands you specify in the scriptblock, more PowerShell jobs run in the background, and more memory consumes.

To run multiple commands,

Start-Job -ScriptBlock{Get-EventLog -LogName System; Get-Process}

You can also use Invoke-Command to run the Job in the background with -AsJob parameter. As InvokeCommand is designed to run commands on the remote computers, you can run background jobs for the remote servers.

Example

$sb = {Get-EventLog -LogName System; Get-Process}
Invoke-Command -ComputerName Test1-Win2k16,Test1-Win2k12 -ScriptBlock $sb -AsJob

Output

You can also specify the name of the job using -JobName parameter.

$sb = {Get-EventLog -LogName System; Get-Process}
Invoke-Command -ComputerName Test1-Win2k16,Test1-Win2k12 -ScriptBlock $sb -
AsJob -JobName NewJob

Output

To check the Job status, use Get-Job command.

Id    Name    PSJobTypeName    State    HasMoreData    Location
--    ----    -------------    -----    -----------    --------
21    NewJob    RemoteJob      Running    True       Test1-Win2k16,Te

The above output is truncated.

Every job has at least one parent job and a child job. The number of child jobs running in the background depends on the number of remote computers. To get all the child jobs, use -IncludeChildJob parameter in Get-Job cmdlet.

Example

Get-Job -IncludeChildJob

Output

Id    Name    PSJobTypeName    State    HasMoreData    Location
--    ----    -------------    -----    -----------    --------
21    NewJob    RemoteJob    Completed    True       Test1-Win2k16,T...
22    Job22                  Completed    True       Test1-Win2k16
23    Job23                  Completed    True       Test1-Win2k12

By the time we ran above command, the job is completed and the same can be visible in State property. Here 22 and 23 job IDs are child jobs which are running on the remote computers and 21 is the parent job.

To get the job output immediately after the job is scheduled, the Receive-Job command is used. This command can be run even the Job status is running.

You need to use Receive-Job command with Job with Name, ID, Job name variable, or any other supported parameter.

Example

Start-Job -Command {Get-Service Spooler, W32Time}
Receive-Job -Name Job5

To retrieve job using ID,

Receive-Job -ID 5
raja
Published on 03-Sep-2020 10:41:58
Advertisements