- Courses
- đź’»Programming Languages
- 🪟PowerShell
- PowerShell Scripting at Scale
- Performance Optimization
- Pipeline Optimization
On this page
PowerShell Pipeline Optimization: Complete Guide
Updated on April 25, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
The PowerShell pipeline is one of the language's most powerful features, enabling elegant, composable commands. However, the pipeline processes objects one at a time, and each stage introduces overhead. Scripts that leverage .NET directly and avoid unnecessary pipeline operations tend to be significantly faster than purely idiomatic PowerShell. The key is knowing when to use the pipeline and when to bypass it for performance-critical code.
Understanding Pipeline Overhead​
The pipeline's object-by-object processing provides flexibility but comes at a cost. Each stage in a pipeline—whether it's Where-Object, ForEach-Object, or Select-Object—processes every object that reaches it. With large datasets, this overhead compounds dramatically.
Important: Many performance techniques described here are not idiomatic PowerShell and may reduce readability. Script authors are advised to use idiomatic PowerShell unless performance dictates otherwise.
The Filter-Left Principle​
The single most important performance rule in PowerShell is filter left. Any filtering should occur as far to the left (as close to the beginning of the command line) as possible.
Why Filter Left Matters​
Consider a container of blocks in different colors with letters printed on them. The task is to sort only the red blocks alphabetically. Which approach is faster?
Inefficient (filter right):
Get-Block | Sort-Object -Property Letter | Where-Object { $_.Color -eq 'Red' }
Efficient (filter left):
Get-Block | Where-Object { $_.Color -eq 'Red' } | Sort-Object -Property Letter
Code explanation: The first command sorts every block—red, green, and blue—before discarding the non-red ones. The second removes unwanted blocks first, so only the remaining red blocks are sorted. When working with thousands of objects, this difference becomes enormous.
Use Native Filtering Parameters Over Where-Object​
Whenever possible, use the filtering parameters built into cmdlets rather than filtering with Where-Object. Native parameters filter at the source—often at the provider or server level—reducing the number of objects entering the pipeline.
File System Filtering​
Inefficient:
Get-ChildItem C:\Windows\Logs | Where-Object { $_.Extension -eq '.log' }
Efficient:
Get-ChildItem C:\Windows\Logs -Filter *.log
Code explanation: The -Filter parameter is processed by the filesystem provider, filtering files before they're returned as objects. The Where-Object approach retrieves every file and then filters them client-side.
Active Directory Filtering​
The difference is even more pronounced with remote data sources:
Inefficient:
Get-ADUser -Filter * | Where-Object { $_.Enabled -eq $true }
Efficient:
Get-ADUser -Filter "Enabled -eq 'True'"
Code explanation: The first command retrieves every user from the domain controller and filters locally. The second filters on the domain controller itself, dramatically reducing network traffic and processing time.
Check for Native Parameters​
Whenever you're tempted to use Where-Object, check the cmdlet's help for filtering parameters like -Filter, -Include, -Exclude, -Name, or type-specific parameters like -File and -Directory.
Avoid the Pipeline for Simple Property Access​
When you only need a single property from a collection, avoid piping to Select-Object. Direct property access is faster and avoids object reconstruction.
Inefficient:
Get-Service | Select-Object -ExpandProperty Name
Efficient:
(Get-Service).Name
Code explanation: Select-Object -ExpandProperty processes each object individually through the pipeline. Direct property access retrieves all property values in a single operation, with significantly lower overhead.
Prefer foreach Loops Over ForEach-Object for Large Collections​
ForEach-Object processes items through the pipeline, which is slower than iterating in memory. A classic foreach loop is often orders of magnitude faster.
Pipeline loop (slower for large collections):
Get-ChildItem C:\Windows\System32 | ForEach-Object { $_.FullName }
Native loop (faster):
foreach ($file in Get-ChildItem C:\Windows\System32) {
$file.FullName
}
Code explanation: The foreach loop loads the entire collection into memory once and iterates efficiently. ForEach-Object processes items as they arrive through the pipeline, which is more memory-efficient for streaming but slower due to per-item overhead.
When to Use ForEach-Object​
ForEach-Object is preferable when:
- Processing data that doesn't fit in memory (streaming large files)
- You need to process items as they arrive
- You're chaining pipeline operations
The .ForEach() Method​
The .ForEach() method on collections is faster than ForEach-Object but doesn't stream output:
(Get-ChildItem C:\Windows\System32).ForEach({ $_.FullName })
Consolidate Pipeline Stages​
Multiple pipeline stages often perform work that a single command could handle natively.
Inefficient:
Get-EventLog -LogName System |
Where-Object { $_.EntryType -eq 'Error' } |
Select-Object TimeGenerated, Message
Efficient:
Get-EventLog -LogName System -EntryType Error |
Select-Object TimeGenerated, Message
Code explanation: Get-EventLog supports filtering by entry type natively, eliminating the need for Where-Object.
Suppress Unnecessary Output​
Writing objects to the pipeline when they're not needed wastes resources. There are several ways to suppress output, with varying performance:
| Method | Performance | Example | | ------------------- | ----------- | --------------------- | --------- | | Assign to $null | Fastest | $null = Get-Process | | Cast to [void] | Fast | [void](Get-Process) | | Redirect to $null | Fast | Get-Process > $null | | Pipe to Out-Null | Slowest | Get-Process | Out-Null |
Code explanation: In tests, assigning to $null and casting to [void] were the fastest, while piping to Out-Null was significantly slower—up to 2.2x slower in some cases.
Note: In PowerShell 7.5 and later, array addition has been optimized and no longer creates a new array for each operation. However, the performance considerations described here still apply to earlier versions.
Use Provider-Specific Features​
Different PowerShell providers offer performance optimizations that the generic pipeline lacks.
File System Provider​
- Use
-Filterinstead ofWhere-Objectfor filename filtering - Use
-Recursewith-Filterto limit returned items - Consider
-Includeand-Excludefor simple patterns
Registry Provider​
- Use
-Pathwith wildcards rather than retrieving all keys and filtering
Active Directory Module​
- Use
-Filteror-LDAPFilteron the cmdlet itself - Use
-Propertiesto request only needed attributes
Common Optimization Mistakes​
Over-Optimizing Prematurely​
Start with readable, idiomatic PowerShell. Only optimize when measurements show it's necessary.
Using Where-Object on Already-Filtered Data​
Don't filter what's already filtered:
# Redundant filtering
Get-ChildItem -File | Where-Object { $_.PSIsContainer -eq $false }
Ignoring .NET Alternatives​
For performance-critical code, consider using .NET classes directly. For example, [System.IO.Directory]::GetFiles() is often faster than Get-ChildItem for simple file listings.
Summary​
Optimizing pipeline operations is one of the most effective ways to improve PowerShell script performance. The key techniques are:
- Filter left — perform filtering as early in the pipeline as possible
- Use native filtering parameters — prefer
-Filterand similar overWhere-Object - Access properties directly — avoid
Select-Object -ExpandPropertyfor simple property access - Choose the right loop — use
foreachfor large collections,ForEach-Objectfor streaming - Consolidate pipeline stages — combine operations where possible
- Suppress output efficiently — use
$nullor[void]instead ofOut-Null
These practices produce faster, more scalable scripts suitable for enterprise automation.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Measuring Execution Time
Next
Using .NET Classes Directly
-
Understanding Pipeline Overhead
-
The Filter-Left Principle
- Why Filter Left Matters
-
Use Native Filtering Parameters Over Where-Object
- File System Filtering
- Active Directory Filtering
- Check for Native Parameters
-
Avoid the Pipeline for Simple Property Access
-
Prefer foreach Loops Over ForEach-Object for Large Collections
- When to Use ForEach-Object
- The .ForEach() Method
-
Consolidate Pipeline Stages
-
Suppress Unnecessary Output
-
Use Provider-Specific Features
- File System Provider
- Registry Provider
- Active Directory Module
-
Common Optimization Mistakes
- Over-Optimizing Prematurely
- Using Where-Object on Already-Filtered Data
- Ignoring .NET Alternatives
-
Summary
