Use this when running PowerShell work in parallel.
Parallel Execution · PowerShell Automation
Parallel execution in PowerShell 7 uses ForEach-Object -Parallel with -ThrottleLimit, while Invoke-Command runs on many remote computers at once.
1. Parallel execution speeds up multi-target work.
2. Set ThrottleLimit to protect targets.
3. Use $using: for variables.
4. Collect errors per item.
5. Thread safety for shared data.
items → parallel runspaces/remoting → results
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Measure a representative sequential baseline, then test the same workload with explicit -ThrottleLimit. Record item identifiers in outputs so results can be correlated despite completion order. Inspect errors per work item and verify final target state/count independently, especially if one terminating error does not cancel all other parallel instances.
Step 2 Common mistakes Narrow the fault
ForEach-Object -Parallelwithout throttle.
Step 3 Where it fails Narrow the fault
Parallel execution can amplify API throttling, lock contention and partial failure. A terminating exception in one parallel scriptblock may not stop siblings. Keep worker isolation, shared-state safety, throttle policy, per-item results and final convergence as separate evidence layers.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- ForEach-Object -Parallel works in PowerShell 7.
- -ThrottleLimit controls concurrency.
- Use $using: to pass variables in.
1..10 | ForEach-Object -Parallel { $_ } -ThrottleLimit 5— PowerShell 7 parallelInvoke-Command -ComputerName <list> -ThrottleLimit 32— remote fan-outStart-ThreadJob— threads
ReferenceWhy it matters1 section · depth on demand
In depth
Parallel execution in PowerShell runs work against many targets concurrently. In PowerShell 7, ForEach-Object -Parallel with a throttle limit is the simplest option; Invoke-Command against many computers is parallel by default (32 at a time).
$servers = Get-Content servers.txt
$results = $servers | ForEach-Object -Parallel {
$r = Test-NetConnection $_ -Port 443 -WarningAction SilentlyContinue
[pscustomobject]@{ Server = $_; Https = $r.TcpTestSucceeded }
} -ThrottleLimit 20
$results | Where-Object { -not $_.Https }
Invoke-Command -ComputerName $servers -ThrottleLimit 50 -ScriptBlock { Get-HotFix | Select-Object -Last 1 }
Variables from the parent scope need $using: inside -Parallel blocks, and shared collections must be thread-safe ([System.Collections.Concurrent.ConcurrentBag[object]]).
ReferenceImportant terms & boundaries1 section · depth on demand
PowerShell mechanics
ForEach-Object -Parallel in PowerShell 7 runs scriptblocks in separate runspaces on process threads and uses a runspace pool by default in current versions. ThrottleLimit controls concurrent scriptblocks for that invocation, $using: exposes caller variables to parallel code, and output/error ordering is nondeterministic across parallel workers. ThreadJob uses a related in-process isolation model with different orchestration surfaces.
ReferenceConfiguration & state1 section · depth on demand
Authoring and lifecycle
Parallelize work only when task duration or I/O wait exceeds runspace coordination overhead. Cap concurrency according to target/API limits, avoid unsynchronized mutation of shared non-thread-safe objects, and make each work item independently retryable where possible. Treat rate limits, connection pools and remote-service quotas as part of the concurrency design.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Configuration & expected output
Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.
Configuration example
Assumptions: Review target identity, permissions and environment before mutation.
$results = $items | ForEach-Object -Parallel { Invoke-Work $_ } -ThrottleLimit 4Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Microsoft Learn documentation reviewed for bounded PowerShell reference claims. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/foreach-object?view=powershell-7.5 · confidence 94%Quick lookup tables
Parallel Execution · PowerShell operational reference
Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.
| Boundary | Parallel Execution reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Deep reference
Healthy vs suspicious evidence
Output interpretation
Output and errors can arrive in nondeterministic order; sequence on screen is not input sequence. $using: can expose references to objects that require thread-safe use, and runspace-local module/session state may differ from the caller. Measure elapsed time plus correctness rather than assuming parallel means faster.