PowerShell Pipeline & Performance
Intermediatev1.0.0
Optimize PowerShell pipeline performance — streaming vs collecting, ForEach-Object -Parallel, Where-Object -FilterScript, avoiding common bottlenecks, and measuring execution time.
Content
Overview
The PowerShell pipeline streams objects between commands — processing one object at a time without loading everything into memory. Understanding when to stream vs collect, and how to parallelize, is the difference between scripts that take seconds and scripts that take hours.
Why This Matters
- -Pipeline streaming processes 1M objects in constant memory
- -Collecting all results first (arrays) uses O(N) memory and is slower
- -ForEach-Object -Parallel enables multi-core processing (PowerShell 7+)
Performance Patterns
Step 1: Pipeline Streaming vs Collection
Step 2: ForEach-Object -Parallel (PowerShell 7+)
Step 3: Filter Left, Format Right
Step 4: Measure Performance
Best Practices
- -Use pipeline streaming for large datasets (don't collect into arrays)
- -Use ForEach-Object -Parallel for I/O-bound operations (network, disk)
- -Filter at the source whenever possible (Get-ADUser -Filter, not Where-Object)
- -Avoid += with arrays in loops (O(N^2) — use List<T> or pipeline)
- -Use Measure-Command to benchmark before and after optimization
- -Format output last (Format-Table/Format-List should be the final pipeline stage)
Common Mistakes
- -Using += to build arrays in loops (creates new array every iteration)
- -Filtering client-side when server-side filtering is available
- -Using Format-* cmdlets in the middle of a pipeline (breaks object output)
- -Not using -ThrottleLimit with -Parallel (defaults to 5, may need tuning)
- -Collecting pipeline output when streaming would work
FAQ
Discussion
Loading comments...