The Pipeline: Where-Object, Select-Object, Sort-Object
By the end of this lesson you'll chain commands together with the pipeline, filter the exact objects you want with Where-Object, reshape and trim them with Select-Object, and order them with Sort-Object — then group and measure them. This is the core skill that turns raw cmdlet output into precise, useful answers.
Learn The Pipeline: Where-Object, Select-Object, Sort-Object in our free PowerShell course — an interactive lesson with worked examples, a practice exercise…
Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Think of the pipeline as a factory conveyor belt . Whole products (objects) ride along it, not crumbs of text. Where-Object is a quality inspector who pushes off any product that fails a test. Select-Object is a packager who keeps only the parts you want and can stamp on a new label (a calculated property). Sort-Object lines the boxes up in order, and Group-Object stacks identical ones together. Because every item stays a full object the whole way down the belt, each station can still read any of its properties.
1. The Object Pipeline
In older shells, | passes text , so you spend your life slicing strings. PowerShell pipes objects instead — every property and method stays intact from one command to the next. That's why you can filter on $_.WorkingSet or sort on $_.CPU without parsing anything. Inside the cmdlets that take a script block, $_ (or its spelled-out twin $PSItem ) is the current object . Read this worked example, then run it.
2. Filtering with Where-Object
Where-Object keeps only the objects whose condition is $true and drops the rest. You write the test with comparison operators : -eq / -ne (equal / not), -gt / -lt / -ge / -le (numeric), -like (wildcards * ? ), and -match (regex). For a single property test, the short comparison-statement form ( Where-Object Status -eq 'Running' ) is cleaner; for compound logic use the script-block form with -and / -or .
Now you try. Fill in the one blank using the hint in the comment, then run it.
3. Shaping output with Select-Object
Select-Object decides what comes out. Use -Property to keep only the columns you need, -First / -Last to take rows from the top or bottom, and -ExpandProperty to unwrap a single property to its raw value (a clean list of strings instead of wrapper objects). The real power tool is a calculated property : a hashtable {"@{ Name = '...'; Expression = }"} that invents a brand-new column on the fly.
A calculated property's hashtable accepts the short keys n (for Name) and e (for Expression): {"@{ n='MB'; e= }"} . Handy when typing at the prompt — but spell out Name and Expression in saved scripts so a future reader instantly understands them.
4. Ordering with Sort-Object
Sort-Object orders objects by one or more properties, ascending by default. Add -Descending to flip it (the classic "biggest first, then take the top N" pattern), pass several property names to sort by multiple keys, and use -Unique to drop duplicate values after sorting.
Your turn again — two blanks. The "sort descending, then take the first N" combination is one you'll use constantly.
Two more cmdlets complete the toolbox. Group-Object buckets items by a shared value and counts each bucket; Measure-Object computes aggregates like Sum, Average, and Maximum; and ForEach-Object runs a script block once per item. Here they all work together on the files in a folder.
Because each file is an object , Group-Object Extension can bucket by the .Extension property and Measure-Object -Property Length can total the .Length — no text parsing anywhere.
No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. Filtering and ranking processes is something you'll do constantly in real work.
Practice quiz
What does the PowerShell pipeline pass between commands?
- Full .NET objects
- File paths only
- Exit codes
- Plain text lines
Answer: Full .NET objects. Unlike traditional shells, PowerShell passes rich .NET objects down the pipeline, keeping every property and method intact.
Inside a Where-Object script block, what does $_ represent?
- The whole pipeline
- The last error
- The current object being evaluated
- The previous command
Answer: The current object being evaluated. $_ (also written $PSItem) is the automatic variable holding the current pipeline object.
Which cmdlet keeps only the objects that match a condition?
- Group-Object
- Where-Object
- Select-Object
- Sort-Object
Answer: Where-Object. Where-Object filters the pipeline, passing through only objects whose condition evaluates to true.
Which comparison operator tests for 'greater than'?
- -lt
- -eq
- -ge
- -gt
Answer: -gt. PowerShell uses word-style operators: -gt is greater than, -lt is less than, -eq is equals.
What does Select-Object -ExpandProperty Name return?
- The raw values of the Name property
- A sorted list
- The count of names
- An object with a Name property
Answer: The raw values of the Name property. -ExpandProperty unwraps a single property to its raw value(s) instead of returning an object that wraps it.
How do you get the 3 largest items after sorting?
- Sort-Object Size; Select-Object -First 3
- Sort-Object Size -Descending; Select-Object -First 3
- Select-Object -Last 3
- Group-Object Size -Top 3
Answer: Sort-Object Size -Descending; Select-Object -First 3. Sort descending so the biggest are first, then take the first 3 with Select-Object -First 3.
Which operator matches text against a wildcard like *.log?
- -contains
- -match
- -eq
- -like
Answer: -like. -like uses wildcard patterns (* and ?). Use -match for regular expressions instead.
What does Sort-Object -Unique do?
- Groups identical objects
- Counts items
- Removes duplicate values after sorting
- Sorts in reverse
Answer: Removes duplicate values after sorting. -Unique removes duplicate entries from the sorted output, leaving one of each value.
Which cmdlet runs a script block once for every item in the pipeline?
- Measure-Object
- ForEach-Object
- Group-Object
- Where-Object
Answer: ForEach-Object. ForEach-Object executes its script block once per pipeline item, with $_ as the current object.
Which cmdlet calculates statistics like Sum, Average, and Maximum?
- Measure-Object
- Select-Object
- Compare-Object
- Group-Object
Answer: Measure-Object. Measure-Object computes aggregates such as -Sum, -Average, -Minimum, and -Maximum over a property.