The Object Pipeline

💤0
Lv 10 XP
← ⚙️ Scripting & Automation · PowerShell

The Object Pipeline

Intermediate ⭐ 80 XP ⏱ 18 min #powershell#pipeline#objects

Filter, select, sort, and transform objects flowing through the PowerShell pipeline.

📖Theory

Because PowerShell pipes objects, you manipulate data by its properties rather than by column position. The core verbs:

  • Where-Object — filter rows by a condition
  • Select-Object — pick properties (columns), or -First/-Last N
  • Sort-Object — order by a property
  • ForEach-Object — run code per item
  • Group-Object / Measure-Object — aggregate

Get-Member reveals which properties and methods an object exposes, so you always know what you can filter or select on.

🌍Real-World Example
# Services that are stopped but set to auto-start
Get-Service |
  Where-Object { $_.Status -eq 'Stopped' -and $_.StartType -eq 'Automatic' } |
  Select-Object Name, DisplayName |
  Sort-Object Name

# Top 3 largest files, as JSON
Get-ChildItem -Recurse -File |
  Sort-Object Length -Descending |
  Select-Object -First 3 Name, Length |
  ConvertTo-Json
✍️Hands-On Exercise
  1. List running services, showing only Name and Status.
  2. Find the three largest files in a folder tree.
  3. Group processes by name and count each group.
  4. Export a filtered list to CSV with Export-Csv.
🧾Cheat Sheet
TaskCmdlet
FilterWhere-Object { $_.Prop -eq x }
Select columnsSelect-Object A, B
Top NSelect-Object -First N
SortSort-Object Prop -Descending
Per-item codeForEach-Object { … }
Group / countGroup-Object Prop
To/from JSONConvertTo-Json / ConvertFrom-Json
💬Common Interview Questions
What does $_ mean in a PowerShell pipeline?

It’s the current pipeline object inside a script block, e.g. Where-Object { $_.Status -eq 'Running' } filters on each incoming object’s Status property.

How do you select specific properties from objects?

Select-Object Name, Status returns objects with just those properties; add -First N or -Last N to limit the count.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type