PowerShell is administrator’s best friend. A long running job can be made to run faster by breaking them into logical functionalities and run those in an asynchronous manner. This will help speed up the processing of tasks from PowerShell.
We usually have the asynchronous operations code written in server languages like C#. Same can be achieved in scripting languages like PowerShell.
For example, when we are dealing with a task to copy large amount of files from multiple source location.
A simple copy operation
- $source = "\\RemoteServer\c$\fileshares"
- $target = "D:\CopyTest"
- Copy-Item -Path $source -Destination $target -Recurse Verbose
- As a next step, we will wrap up this in a Start-Job
- $copyJob = Start-Job –ScriptBlock {
- $source = "\\RemoteServer\c$\fileshares"
- $target = "D:\CopyTest"
- Copy-Item -Path $source -Destination $target -Recurse Verbose
- }
- Wait-Job $copyJob
Waiting for job to complete
We can wait for a job to complete using Receive-Job.
- Receive-Job $copyJob
To carry out multiple copy operations asynchronously:
- $copyJob1 = Start - Job– ScriptBlock
- {
- $source = "\\RemoteServer\c$\fileshares"
- $target = "D:\CopyTest"
- Copy - Item - Path $source - Destination $target - Recurse Verbose
- }#
- Code here will run async
- $copyJob2 = Start - Job– ScriptBlock
- {
- $source = "\\RemoteServer\c$\Shared"
- $target = "D:\CopyTest"
- Copy - Item - Path $source - Destination $target - Recurse Verbose
- }#
- Code here will run async
- Wait - Job $copyJob1
- Wait - Job $copyJob2

nitish bijalwanPosted Feb 9, 2018, 12:35 AM
Great post. Thanks for sharing the info
Kumaresh RajalingamPosted Feb 14, 2016, 5:11 AM
Nice one
Santhakumar MunuswamyPosted Jan 9, 2016, 3:06 AM
Thanks for nice share
Vivek BansodPosted Jan 8, 2016, 6:32 AM
Good One..