Pipeline
PowerShell · Reference cheat sheet
Pipeline
PowerShell · Reference cheat sheet
📋 Overview
The PowerShell pipeline passes objects (not text) between commands. ByValue and ByPropertyName binding decide how properties map to the next cmdlet’s parameters. Use ForEach-Object, Where-Object, and Select-Object as the core filters.
🔧 Core concepts
| Piece | Role |
|---|---|
| | Send objects downstream |
| ByValue | Entire object matches parameter type |
| ByPropertyName | Property name matches parameter name |
$_ / $PSItem | Current pipeline object |
ForEach-Object (%) | Per-object script |
Where-Object (?) | Filter |
Select-Object | Project / expand properties |
Tee-Object | Copy to variable/file and continue |
Begin/Process/End blocks in advanced functions enable true streaming.
💡 Examples
Filter and project:
Get-ChildItem .\logs -File |
Where-Object { $_.Length -gt 1MB } |
Select-Object Name, Length, LastWriteTime |
Sort-Object Length -DescendingForEach and Tee:
Get-Service |
Where-Object Status -eq 'Running' |
ForEach-Object { $_.Name } |
Tee-Object -Variable namesProperty binding:
# Stop-Process binds -Id ByPropertyName
Get-Process notepad -ErrorAction SilentlyContinue |
Stop-Process -WhatIfCollect vs stream:
# Parentheses collect all output first
(Get-ChildItem -Recurse).Count⚠️ Pitfalls
- Wrapping a pipeline in
(…)buffers everything—can exhaust memory on large sets. - Native executables receive text; format may break object pipelines.
Format-*cmdlets emit formatting objects—don’t pipe them to exporters; useSelect-Objectfirst.ForEach-Objectvsforeachstatement: the statement doesn’t stream in the same way.- Nulls in the pipeline can surprise
-eqfilters; be explicit. - Pipeline-bound parameters need correct attributes in custom functions.