Processing a Queue using Parallel PowerShell Jobs with Throttling

It is often necessary to repeat the same code with different arguments and use PowerShell jobs to speed up the process. Depending on the resources required by the code it may be necessary to limit parallel execution to a certain number of parallel jobs. In this post, I will present code to use PowerShell jobs to process such a queue with throttling.

Let’s assume it is necessary to remove a registry key on a large number of servers: server0, server1, ..., server9. The folloing code demonstrates how to achieve this with my cmdlet called Invoke-Queue:

$Servers = @(server0, server1, server2, server3, server4, server5, server6, server7, server8, server9)
$Jobs = Invoke-Queue -InputObject $Servers -ThrottleLimit 2 -Scriptblock {
    param(
        $ComputerName
    )
    Invoke-Command -ComputerName $ComputerName -Scriptblock {Remove-Item -Path HKLM:\Software\Test}
}

Invoke-Queue returns an array of PowerShell jobs for checking the outcome of the individual jobs. Please note that Invoke-Queue returns as soon as the last jobs is started. It is your responsibility to wait for all jobs to complete.

It is also possible to pass global arguments using the -ArgumentList parameter. These parameters are appended to the object from the queue:

$RegPath = 'HKLM:\Software\Test'
$Servers = @(server0, server1, server2, server3, server4, server5, server6, server7, server8, server9)
$Jobs = Invoke-Queue -InputObject $Servers -ArgumentList $RegPath -ThrottleLimit 2 -Scriptblock {
    param(
        $ComputerName,
        $RegPath
    )
    Invoke-Command -
    Remove-Item -Path $RegPath
}

When a huge number of input objects is supplied it is helpful to receive progress information. By specifying the -ShowProgress switch, Invoke-Queue will display a progress bar (see screenshot below).

Invoke-Queue(Progress)

The following code implements the backend for processing the queue ($Servers) and for throttling the parallelization:

In case you have any feedback use the comments below or create a new revision of the code in GitHub.

Feedback is always welcome! If you'd like to get in touch with me concerning the contents of this article, please use Twitter.