Working with Objects
PowerShell's object pipeline โ select, filter, sort, and format structured data.
What You'll Learn
- How PowerShell pipes objects (not text) between commands
- Select-Object, Where-Object, Sort-Object, and ForEach-Object
- Discovering properties/methods with Get-Member
- Formatting and exporting data (CSV, JSON, XML)
The Object Pipeline
This is PowerShell's superpower. When you pipe commands together with |, you're not passing raw text โ you're passing .NET objects with properties and methods. This means you can filter by any property, sort by any field, and select exactly what you need.
๐ฏ Real-World Analogy: Bash pipelines are like passing notes written on paper โ you have to parse the text yourself. PowerShell pipelines are like passing spreadsheets โ columns, types, and sorting are built in.
# The Big Three pipeline cmdlets:
Get-Process | Where-Object { $_.CPU -gt 10 } # FILTER
Get-Process | Sort-Object CPU -Descending # SORT
Get-Process | Select-Object Name, CPU, Id # PROJECT
# Chain them for powerful one-liners:
Get-Process | Where-Object CPU -gt 10 | Sort-Object CPU -Desc | Select-Object -First 5The $_ variable represents the current object in the pipeline (like "this item" in a loop).
Object Pipeline
See how objects flow through Select, Where, and Sort.
// PowerShell Object Pipeline โ simulated in JavaScript
console.log("=== The Object Pipeline ===");
console.log();
console.log("In bash, pipes pass TEXT between commands.");
console.log("In PowerShell, pipes pass OBJECTS with properties & methods.");
console.log();
// Simulate Get-Process output
const processes = [
{ Name: "chrome", CPU: 45.2, Memory: 512000, Id: 1234 },
{ Name: "code", CPU: 23.1, Memory: 384000, Id: 2345 },
{ Name: "node", CPU: 12.8, Memory: 256000, Id: 3456 },
...Exploring & Formatting Objects
Use Get-Member (alias gm) to discover what properties and methods an object has:
"Hello" | Get-Member # String methods: ToUpper, Split, Replace... Get-Date | Get-Member # DateTime properties: Year, Month, DayOfWeek... Get-Process | Get-Member # Process: Name, CPU, Id, WorkingSet...
PowerShell has four main format cmdlets for controlling output display:
Format-Table(ft) โ Columnar table viewFormat-List(fl) โ Vertical key:value listFormat-Wide(fw) โ Single property in wide columnsOut-GridViewโ Interactive GUI table (Windows)
โ ๏ธ Common Mistake
Never pipe Format-* output into further processing. Format cmdlets produce display objects, not data. Always put Format-* or Out-* as the last command in your pipeline.
Members & Formatting
Discover object members and format output.
// Formatting & Members โ simulated in JavaScript
console.log("=== Exploring Object Members ===");
console.log();
console.log("Every object has properties and methods.");
console.log("Use Get-Member to discover them:");
console.log();
console.log('PS> "Hello" | Get-Member');
console.log(" TypeName: System.String");
console.log(" Methods: Contains, EndsWith, IndexOf, Replace,");
console.log(" Split, StartsWith, Substring, ToLower,");
console.log(" ToUpper, Trim, PadLe
...๐ก Pro Tip
Use Measure-Object for quick calculations: Get-Process | Measure-Object CPU -Sum -Average -Maximum gives you totals and stats in one go. Combine with Group-Object for powerful data analysis right from the shell.
๐ Quick Reference
| Cmdlet | Alias | Purpose |
|---|---|---|
| Where-Object | where / ? | Filter objects |
| Select-Object | select | Pick properties / first N |
| Sort-Object | sort | Sort by property |
| ForEach-Object | foreach / % | Process each item |
| Get-Member | gm | Discover properties/methods |
| Measure-Object | measure | Count, sum, average |
๐ Lesson Complete!
You now understand PowerShell's object pipeline โ the feature that makes it uniquely powerful. Next up: writing reusable functions and scripts.
Sign up for free to track which lessons you've completed and get learning reminders.