🎓 A1Learner
🔍
0 of 46 lessons completed

PowerShell Parallel Pitfalls

A parallel pitfall is a mistake that only shows up when work runs at the same time. Separate workers do not share your variables, so this lesson shows three traps and the safe pattern for each.

Changes inside a worker stay inside

Each worker gets a copy of the outside total. The outside total is unchanged after the work finishes.

$total = 100
$seen = 1..3 | ForEach-Object -Parallel { $using:total + $_ }
($seen | Sort-Object) -join ", "
"total after: $total"

Appending inside a worker does not collect

Collect results from the pipeline output instead of appending inside a worker. Appending inside leaves the outside list empty.

$collected = @()
1..3 | ForEach-Object -Parallel { $collected += $_ } | Out-Null
"collected count: $($collected.Count)"

Give each worker its own file

One file per worker avoids overwrites. The listing below is sorted, so the file names print in a fixed order.

1..3 | ForEach-Object -Parallel { Set-Content -Path "part-$_.txt" -Value "value $_" }
Get-ChildItem -Filter "part-*.txt" | Sort-Object Name | Select-Object -ExpandProperty Name
Get-Content -Path "part-2.txt"

Example

$base = 10
$results = 1..4 | ForEach-Object -Parallel { $using:base + $_ } -ThrottleLimit 2
($results | Sort-Object) -join ", "
"base after: $base"

Key points

  • Shared variable: a variable that two workers both try to change.
  • Use $using: to give a parallel worker a copy of an outside value.
  • A copy is not the original, so changes inside a worker do not change the outside variable.
  • Use -ThrottleLimit to set how many workers run at the same time.
  • Use a separate file per worker when workers must write files.

In this example, four workers each get a copy of the base value, at most two run at the same time, the results are sorted before printing, and the base value is unchanged after the work finishes.

Note

A worker that writes to the same file as another worker can overwrite its work.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A parallel worker adds 5 to an outside total of 100. What is the outside total after the parallel work finishes?