1

I am trying a very simple PowerShell Parallel example as specified in the documentation.

1..5 | ForEach-Object -Parallel { "Hello $_"; sleep 1; } -ThrottleLimit 5 

But its giving the following error, any help in this regards would be appropriated.

ForEach-Object : Parameter set cannot be resolved using the specified named parameters.
At C:\temp\parallel.ps1:2 char:9
+ 1..10 | ForEach-Object -Parallel {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo : MetadataError: (:) [ForEach-Object], ParameterBindingException
 + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.ForEachObjectCommand

Following is my PowerShell Version information

Name Value
---- -----
PSVersion 5.1.18362.752
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.752
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
asked May 22, 2020 at 13:20

3 Answers 3

5

ForEach-Object -Parallel has been introduced in PowerShell 7.0. You are using PowerShell 5.1. That's why it doesn't work.

If you have PowerShell 7.0 installed in parallel (which is possible), call the PS executable directly and execute your script with that.

answered May 22, 2020 at 13:34
Sign up to request clarification or add additional context in comments.

Comments

4

The documentation shows powershell 7 by default, which could be confusing.

A start-job alternative, but it's multi-process:

1..5 | ForEach-Object { start-job { 
 "Hello $args"; sleep 1 } -args $_ } | receive-job -wait -auto

You could download threadjob in powershell 5, which is faster than start-job. https://www.powershellgallery.com/packages/ThreadJob. It comes with powershell 7. It works like foreach-object -parallel.

1..5 | ForEach-Object { start-threadjob { 
 "Hello $using:_"; sleep 1 } } | receive-job -wait -auto
answered May 22, 2020 at 13:47

Comments

2

As the previous answers specified, Foreach-Object -Parallel is available in PowerShell 7. Still if you need to execute commands in parallel, you may try to explore workflows. More details, could be found in PowerShell help: Get-Help about_Workflows -ShowWindow.

answered May 22, 2020 at 13:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.