Does powershell's parallel foreach use at most 5 thread? Does powershell's parallel foreach use at most 5 thread? powershell powershell

Does powershell's parallel foreach use at most 5 thread?


I believe your test is a little flawed. According to this TechNet article, $PID is not an available variable in a Script Workflow.

A better test with an explanation can be found on this site

Basically, the ThrottleLimit is set to a theoretical [Int32]::MaxValue. The poster designed a better test to check parallel processing also (slightly modified):

Workflow Throttle-Me {    [cmdletbinding()]    param (        [int]$ThrottleLimit = 10,        [int]$CollectionLimit = 10    )    foreach -parallel -throttlelimit $ThrottleLimit ($n in 1..$CollectionLimit){        "Working on $n"        "{0:hh}:{0:mm}:{0:ss}" -f (Get-Date)    }}


I can't speak for that specific cmdlet , but as you found PowerShell only uses one thread per PID. The common ways of getting around this are PSJobs and Runspaces. Runspaces are more difficult than PSJobs, but they also have significantly better performance, which makes them the popular choice. The downside is that your entire code has to resolve around them which means an entire rewrite in most cases.

Unfortunately as much as Microsoft pushes PowerShell, it is not made for performance and speed. But it does as good as it can while tied to .NET