Parallel and ThrottleLimit Parameters added to ForEach-Object in PowerShell 7 Preview 3

Preview 3 of PowerShell 7 was released yesterday. It can be downloaded from the PowerShell repository on GitHub. Be sure to choose the preview version for the appropriate operating system. It should go without saying that anything with preview in its name should NOT be installed on a mission-critical production system.

The examples shown in this blog article are being run on a system running Windows 10 (x64). Your mileage may vary with other operating systems and/or versions.

One of the hot new features in this release is the addition of the ability to run ForEach-Object in parallel with the new Parallel parameter. This is different than running foreach in parallel in a workflow in Windows PowerShell.

First, I’ll start by using the range operator to simply sleep 10 times for one second each as shown in the following example. Measure-Command is used to determine how long the command takes and I’ll use the dotted-notation syntax style to return only the total seconds.

The same command is used in the next example except the Parallel parameter has been specified. You would think this should finish is about a second or so depending on the overhead, but over two seconds seems too long, although it is indeed roughly 5 times faster than the previous example.

The answer to why the previous command took over two seconds is because a ThrottleLimit parameter has also been added and it defaults to a limit of 5 threads at a time. Divide the 10 times the previous command is being run by the throttle limit of 5 and that’s why it took over two seconds for the previous example to complete.

In the next example, I’ll specify 10 as the value for the ThrottleLimit parameter since I know the command is going to run 10 times.

Now those results are more like what was expected. Run something for one second 10 times in parallel and it takes about a second plus some overhead to complete.

In addition to the parameters shown in this blog article, ForEach-Object also adds TimeoutSeconds and AsJob parameters in PowerShell 7 Preview 3.

You can find more information about what’s new in PowerShell 7 Preview 3 in this article that was posted on the PowerShell team blog yesterday.

Thoughts, questions, comments? Please post them as a comment to this blog article.

µ

3 Comments

  1. TonyA

    This is very cool. Almost appears to eliminate the need to use jobs at all, with their scripting overhead — setup, looping thru looking for completed, grabbing results, tear down, etc.

    Reply
    • doenoe

      I dont agree on the eliminating Powershell jobs part. Powershell jobs run on the background and don’t necessarily leave you waiting before finishing to continue the rest of the code, this is still the case with foreach-object parallel.

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: