Category filter

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 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

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:

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:

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

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.

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.

  • Sample Script Repository