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 8, 2018, 7:35 PM EST
Great post. Thanks for sharing the info
Kumaresh RajalingamPosted Feb 14, 2016, 12:11 AM EST
Nice one
Santhakumar MunuswamyPosted Jan 8, 2016, 10:06 PM EST
Thanks for nice share
Vivek BansodPosted Jan 8, 2016, 1:32 AM EST
Good One..