PowerShell Pipeline
The pipeline is the heart of PowerShell. The pipe character | takes the output of the command on its left and feeds it, one object at a time, into the command on its right. Chaining small commands this way lets you build powerful one-liners from simple parts.
1..10 | Where-Object { $_ % 2 -eq 0 } | ForEach-Object { $_ * $_ }4
16
36
64
100
Read it from left to right: produce the numbers 1 to 10, keep the even ones, square each of them. Inside the script blocks { … }, the automatic variable $_ (also spelled $PSItem) is "the current object in the pipeline".
Objects, not text
Because objects flow through the pipe, each command can work with properties by name. Here we create some files, then let the pipeline sort them by size and pick properties to show:
Set-Content ./small.txt 'hi'
Set-Content ./medium.txt ('x' * 50)
Set-Content ./large.txt ('y' * 500)
Get-ChildItem ./*.txt |
Sort-Object Length -Descending |
Select-Object Name, Length
Name Length
---- ------
large.txt 501
medium.txt 51
small.txt 3
No column counting, no text parsing: Sort-Object Length sorts by the Length property of each FileInfo object. A long pipeline can be split over several lines by ending each line with |.
The most common pipeline cmdlets
| Cmdlet | Alias | Does |
|---|---|---|
Where-Object |
where, ? |
keeps objects that match a condition |
ForEach-Object |
foreach, % |
runs code for each object |
Select-Object |
select |
picks properties, or the first/last N objects |
Sort-Object |
sort (Windows) |
sorts by one or more properties |
Group-Object |
group |
groups objects with the same value |
Measure-Object |
measure |
counts, sums, averages |
Out-File / Set-Content |
writes to a file |
Each gets its own lesson later. They all work on any kind of object, which is why learning them once pays off everywhere.
Simplified syntax
For simple property comparisons, Where-Object and ForEach-Object accept a shorter form without a script block:
$people = @(
[pscustomobject]@{ Name = 'Ada'; Age = 36 }
[pscustomobject]@{ Name = 'Linus'; Age = 28 }
[pscustomobject]@{ Name = 'Grace'; Age = 45 }
)
$people | Where-Object Age -gt 30 | ForEach-Object Name
"---"
$people | Where-Object { $_.Age -gt 30 -and $_.Name -like 'G*' } | ForEach-Object { $_.Name.ToUpper() }Ada
Grace
---
GRACE
Use the short form for one condition and the script-block form when you need several conditions or calculations.
Streaming: one object at a time
The pipeline doesn't wait for the first command to finish. Each object travels through the whole chain before the next one starts. You can see the order by printing from inside each stage:
1..3 |
ForEach-Object { "produce $_"; $_ } |
Where-Object { $_ -ne 2 } |
ForEach-Object { " consume $_" } consume produce 1
consume 1
consume produce 2
consume produce 3
consume 3
Look closely: each "produce" string was also output by the first stage, so it travelled down the pipe like any other object, passed Where-Object (it isn't equal to 2) and was printed by the last stage as consume produce 1. Everything a stage outputs goes down the pipe, which is a very common surprise. Use Write-Host for messages that should not enter the pipeline:
1..3 |
ForEach-Object { Write-Host "produce $_"; $_ } |
Where-Object { $_ -ne 2 } |
ForEach-Object { " consume $_" }produce 1
consume 1
produce 2
produce 3
consume 3
Now the order is clear: 1 is produced and consumed, 2 is produced and filtered out, 3 is produced and consumed. Streaming is why Get-ChildItem -Recurse | Select-Object -First 5 can stop after five files instead of scanning the whole disk.
Note:
$_only exists inside a pipeline script block. Outside of one it is empty, soWrite-Output $_on its own prints nothing.
Test yourself
What does $_ refer to inside ForEach-Object { ... }?
- The previous result
- The current pipeline object
- The script's arguments
- The last error
What does 1..5 | Where-Object { $_ -gt 3 } output?
- 1 2 3
- 4 5
- 3 4 5
- True True
Why does Select-Object -First 1 stop a long pipeline early?
- It sorts first
- The pipeline streams objects one at a time, so upstream commands can be stopped
- It reads the file backwards
- It doesn't; it waits for all objects