PowerShell Lesson 3: The Pipeline — Filtering, Sorting, Selecting and Custom Objects

PowerShell Lesson 3: The Pipeline — Filtering, Sorting, Selecting and Custom Objects featured illustration

📎 This article includes 3 downloadable practice files ↓

⏱ 2 min readUpdated 18 November 2026

📘 PowerShell Beginner Course · Lesson 3 of 6

Advertisement
In this article
  1. Basic Level: The Holy Trinity — Where, Select, and Sort
  2. Intermediate Level: Calculated Properties and Memory Formatting
  3. Expert Level: Pipeline Streaming vs Collection Buffering
  4. Practice Challenge
  5. Continue learning

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 (alias select): Chooses specific properties, limits count (-First, -Last), or removes duplicates (-Unique).
  • Sort-Object (alias sort): 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):

Advertisement
# 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:

Advertisement
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)

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.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Comment