PWSH ForEach-Object -Parallel
When using PowerShell and multiple commands that can be executed in parallel use ForEach-Object -Parallel {}
.
Tips:
- Use
$_
to access the current element - Use
using:
syntax to access variables defined and assigned outside the parallel scope, e.g.$using:MyVariable
A clean example from the documentation is:
$Message = "Output:"
1..8 | ForEach-Object -Parallel {
"$using:Message $_"
Start-Sleep 1
} -ThrottleLimit 4
Output: 1
Output: 2
Output: 3
Output: 4
Output: 5
Output: 6
Output: 7
Output: 8
- Previous: Cron expression
- Next: Worktree