
📎 This article includes 3 downloadable practice files ↓
In this article
The pipeline is the crown jewel of PowerShell. Denoted by the vertical pipe character |, the pipeline passes real, strongly-typed .NET objects from the output of one cmdlet into the input of the next. Understanding how to filter, project, and sort pipeline objects is what transforms occasional command-line users into enterprise automation developers.
Basic Level: The Holy Trinity — Where, Select, and Sort
Three core cmdlets govern 80% of all pipeline data processing:
Where-Object(alias?): Filters objects based on boolean condition criteria.Select-Object(aliasselect): Chooses specific properties, limits count (-First,-Last), or removes duplicates (-Unique).Sort-Object(aliassort): Reorders objects in ascending or descending order across one or multiple properties.
# Find the top 5 largest files in your Downloads folder
Get-ChildItem -Path ":USERPROFILEDownloads" -File |
Sort-Object -Property Length -Descending |
Select-Object -First 5 -Property Name, Length, LastWriteTime
Intermediate Level: Calculated Properties and Memory Formatting
Raw file objects report their size in bytes (e.g., 104857600). In professional reporting, you want formatted human-readable measurements (e.g., 100 MB). PowerShell allows you to define calculated properties on the fly using a hash table with Name (or n) and Expression (or e):
# Transform byte count into formatted Megabytes (MB)
Get-ChildItem -Path "C:WindowsSystem32" -Filter *.dll |
Select-Object -First 10 -Property Name,
@{ Name = "SizeMB"; Expression = { [Math]::Round(.Length / 1MB, 2) } },
LastWriteTime |
Sort-Object -Property SizeMB -Descending
Expert Level: Pipeline Streaming vs Collection Buffering
In PowerShell, some cmdlets stream objects one-by-one as they are produced, while others buffer the entire dataset into memory before passing anything downstream.
| Cmdlet | Pipeline Behavior | Performance Impact |
|---|---|---|
Where-Object |
Streaming (One-by-one) | Minimal memory, starts immediately |
ForEach-Object |
Streaming (One-by-one) | Minimal memory, instant feedback |
Sort-Object |
Buffering (Blocking) | Must read ALL items before sorting |
Group-Object |
Buffering (Blocking) | Must hold complete dataset in memory |
# Efficient streaming: filter BEFORE sorting to keep memory low
Get-ChildItem -Path "D:Data" -File -Recurse |
Where-Object { .Length -gt 50MB } | # Stream and filter early
Sort-Object -Property Length -Descending # Only sort matching large files
Practice Challenge
Write a one-line pipeline command to list all stopped Windows services that have their StartType set to Automatic:
Get-Service | Where-Object { .Status -eq 'Stopped' -and .StartType -eq 'Automatic' } |
Select-Object -Property Name, DisplayName, StartType
📎 Practice files for this article
⬇ Download all 3 files (ZIP · 1,021 B)
- 🖥️ps-03-pipeline.ps1Lesson code file.⬇ PS1 · 432 B
- 🧾sales.csv40 sample orders used by the scripts.⬇ CSV · 1 KB
- 📄ps-03-pipeline-output.txtOutput from our test run.⬇ TXT · 190 B
Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.
Stuck on a step? Ask a question and the AI answers using this article.