# Script to start a background job in PowerShell on Windows

A background job in PowerShell is a process that runs independently of the current session, allowing users to execute tasks without interrupting their work. The Start-Job cmdlet initiates a new PowerShell background job on Windows devices. This feature is particularly useful for executing intensive tasks or lengthy scripts. It allows to run cmdlets, functions, scripts, and command-based tasks in the background, while enabling the primary session to proceed with other tasks without waiting. A script can be used to initiate background jobs in PowerShell on Windows devices. IT Admins can use Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) remote action to deploy this script to devices.

Scripting language – PowerShell

File extension – .ps1

 Disclaimer: 
The sample scripts provided below are adapted from a third-party open-source sites.

 

PowerShell script to start a background job
-------------------------------------------

Script to start a background job in PowerShell on Windows

Start-Job -ScriptBlock { <commands or scripts> }

   1

  Start-Job -ScriptBlock { <commands or scripts> }

   

 

  

Inside the script block, place the PowerShell commands or script code that must run in the background.

A background task will be established when the above command is executed. The user can check the status of the job with the **Get-Job** cmdlet.

The following script helps to start a background job that runs on the local computer.

#### Example 1:

Start-Job -ScriptBlock { Compress-Archive -Path C:\\path\\to\\files\\\* -DestinationPath C:\\path\\to\\output.zip }

   1

  Start-Job -ScriptBlock { Compress-Archive -Path C:\\path\\to\\files\\\* -DestinationPath C:\\path\\to\\output.zip }

   

 

  

Here, the script starts a background job in PowerShell to compress many files using the *Compress-Archive* cmdlet.

**C:\\path\\to\\files** is the path to the directory that contains the files you want to compress. You should replace this with the actual path to the directory on your system.

**C:\\path\\to\\output.zip** is the path to the ZIP archive file that will be created by the *Compress-Archive* cmdlet. You should replace this with the actual path where you want the ZIP archive to be saved.

#### Example 2:

Start-Job -ScriptBlock { Get-Service -Name wuauserv }

   1

  Start-Job -ScriptBlock { Get-Service -Name wuauserv }

   

 

  

Here, the script starts a background job in PowerShell to retrieve information about the process **wuauserv** (Windows Update Service).

$job = Start-Job -ScriptBlock { Get-Service -Name wuauserv } Wait-Job $job Receive-Job $job

   1

2

3

  $job = Start-Job -ScriptBlock { Get-Service -Name wuauserv }

Wait-Job $job

Receive-Job $job

   

 

  

**Wait-Job** sets the wait time to run the command inside the script block. After this, **Receive-Job** will retrieve the output and display it.

[![Use scripts to start a background job in PowerShell and verify its output from Hexnode.](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/04/Start-a-background-job-in-PowerShell-with-scripts-and-verify-its-output.png "Start a background job in PowerShell with scripts and verify its output")](https://cdn.hexnode.com/mobile-device-management/help/wp-content/uploads/2024/04/Start-a-background-job-in-PowerShell-with-scripts-and-verify-its-output.png)

What happens at the device end?
-------------------------------

After the successful execution of the script, the job specified in the command will commence in the background, ensuring no disruption to other ongoing processes on the device.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.