Foreach -parallel (Part 2: Windows PowerShell)

by Dec 10, 2019

PowerShell 7 comes with a new ForEach-Object that supports parallel execution:

1..100 | ForEach-Object -ThrottleLimit 20 -Parallel { Start-Sleep -Seconds 1 $_ }

If you use Windows PowerShell, you can use the same parallelization techniques. For example, download and install this free module:

Install-Module -Name PSParallel -Scope CurrentUser -Force 

It comes with a single command: Invoke-Parallel. You would use it like this:

1..100 | Invoke-Parallel -ThrottleLimit 20 -Scrip-tBlock { Start-Sleep -Seconds 1 $_ } 

Since Invoke-Parallel uses the same techniques as PowerShell 7, the same limitations apply: each thread is executed in its own thread and has no access to local variables. In an upcoming tip, we’ll look at some interesting use cases.

You are a PowerShell Professional, passionate about improving your code and skills? You take security seriously and are always looking for the latest advice and guidance to make your code more secure and faster? You’d love to connect to the vibrant PowerShell community and get in touch with other PowerShell Professionals to share tricks and experience? Then PowerShell Conference EU 2020 might be just the right place for you: (June 2-5, 2020 in Hanover, Germany).

It’s a unique mixture of classic conference with three parallel tracks filled with fast-paced PowerShell presentations, and advanced learning class with live discussions, Q&A and plenty of networking.

Secure your seat while they last: Help build the agenda and make this “your” event by submitting hypothetical sessions you’d like to hear: And if you’d like to present yourself and join the speakers’ team, submit proposals:

Twitter This Tip! ReTweet this Tip!