- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to run PowerShell commands in Background?
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
- Related Articles
- How to run PowerShell commands from the command prompt?
- How to use PowerShell Help commands?
- How to Re-run Last Executed Commands in Linux?
- How to run Invoke-Command in PowerShell Workflow?
- How To Run Commands On Multiple Remote Servers Simultaneously
- How to run an Android service always in background?
- How to Run Postman Collection on Jenkins using Newman Commands?
- How to use Swift to run in background to provide current location?
- How to run a timer in background within your iOS app
- How to run an Android service always in the background using Kotlin?
- Running Multiple Commands in the Background on Linux
- How to run a PowerShell script from the command prompt?
- How to change the background color of the font in PowerShell?
- How to get the list of all the commands available in the PowerShell?
- How to run matplotlib in Tkinter?
