PowerShell 7 New Feature: ForEach-Object -Parallel

Prior to PowerShell version 7 there were a number of different methods for running PowerShell tasks in parallel, for example creating your own runspaces manually or using a third-party module such as Invoke-Parallel or PoshRSJob to make that a little more straightforward.

Not to be confused of course with the foreach -parallel language construct, which exists, but is only available via a Windows PowerShell workflow.

For an excellent summary of the different options prior to v7, I would highly recommend checking out Richard Siddaway’s talk from PSDayUK 2019 which provides some great insight.

In this article we’ll look at how things were prior to this new feature in PowerShell 7 and why you might want to consider using the new Parallel feature to make things simpler.

First of all, the following example demonstrates how to use PowerShell runspaces to process some data in parallel - this is an approach we could have taken prior to PowerShell 7:

You’ll observe that quite a lot of code was required to instantiate using the runspaces!

In PowerShell 7 we can reduce the above example to the following:

Of course bear in mind the above are contrived examples and using the new -Parallel functionality in v7 (or runspaces in earlier versions) may not always be the most effective approach. Typically running in parallel is geared towards code which requires intensive processing over a significant time period or contains elements of waiting which might cause bottlenecks if running in serial fashion. If this is not the case, then it may prove counter intuitive to make use of parallel functionality since there is an overhead every time a runspace is created.


This post is part of the #PS7Now series of blog posts to accompany the release of PowerShell 7. You can find posts from other contributors at the following sites: