Learn › PowerShell

PowerShell Pipeline and Objects: Filter, Sort, Select and Export

The PowerShell pipeline explained: filter, sort, select and group objects with Where-Object, Sort-Object and Select-Object, then export to CSV.

Beginner 10 min read
By Gopinath Karthikeyan

In most shells, commands pass text to each other, and you cut it up with tools like find. PowerShell passes objects – structured data with named properties. That one difference is why PowerShell is so good for admin reports.

See what an object contains

Get-Service -Name Spooler | Get-Member

Get-Member lists the properties (data, like Status, StartType) and methods (actions, like Stop()). To see every property with its value:

Get-Service -Name Spooler | Select-Object *

The four commands you’ll use daily

Where-Object – keep only what matches

Get-Service | Where-Object Status -eq 'Stopped'
Get-Process | Where-Object { $_.WS -gt 500MB }
Get-ChildItem C:\Logs -File | Where-Object LastWriteTime -lt (Get-Date).AddDays(-30)

$_ means “the current object in the pipeline”. Comparison operators: -eq, -ne, -gt, -lt, -ge, -le, -like (wildcards), -match (regex), -in.

Filter early: if a cmdlet has a -Filter or -Name parameter, use it – it’s much faster than fetching everything and filtering afterwards.

Sort-Object

Get-Process | Sort-Object WS -Descending
Get-ChildItem C:\Logs | Sort-Object Length -Descending

Select-Object – choose columns, limit rows

Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, Id, CPU

Calculated properties add your own columns:

Get-PSDrive -PSProvider FileSystem | Select-Object Name,
    @{ Name = 'UsedGB'; Expression = { [math]::Round($_.Used / 1GB, 1) } },
    @{ Name = 'FreeGB'; Expression = { [math]::Round($_.Free / 1GB, 1) } }

Group-Object and Measure-Object

Get-Service | Group-Object Status                       # how many running vs stopped
Get-ChildItem C:\Logs -File | Measure-Object Length -Sum -Average
The PowerShell pipeline passes objects from one command to the next Get-Service Get-Service 250 serviceobjects 250 service objects Where-Object Status -eq Running Where-Object Status -eq Running 120 objects 120 objects Sort-Object Name Sort-Object Name 120, sorted வரிசையில்120 Select-Object Name, Status Select-Object Name, Status 120 × 2properties 120 × 2 properties Export-Csv services.csv Export-Csv services.csv A CSV file ஒரு CSV file Objects, not text: every step keeps all the properties Text அல்ல, objects: ஒவ்வொருபடியிலும் properties இருக்கும்
Each command receives objects, filters or shapes them, and passes objects on. Format and export only at the end.ஒவ்வொரு command-உம் objects-ஐ பெற்று, filter செய்து, அடுத்ததற்கு objects-ஆகவே அனுப்புகிறது. Format/export கடைசியில் மட்டும்.

Export the result

Get-Service | Where-Object StartType -eq 'Automatic' |
    Select-Object Name, DisplayName, Status |
    Export-Csv C:\Temp\auto-services.csv -NoTypeInformation

Import-Csv C:\Temp\auto-services.csv | Where-Object Status -ne 'Running'

Other useful outputs:

... | ConvertTo-Html | Out-File C:\Temp\report.html   # quick HTML report
... | ConvertTo-Json | Out-File C:\Temp\report.json   # for other tools
... | Out-GridView                                     # sortable window (Windows only)

Format- commands go last

Format-Table and Format-List are for display only. They turn objects into formatting instructions, so nothing useful can come after them:

Get-Service | Format-Table Name, Status        # fine on screen
Get-Service | Format-Table | Export-Csv x.csv  # wrong – exports formatting junk

Rule: Select for data, Format only as the very last step on screen.

Put it together: a real report

Disks below 15% free on this server:

Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" |
    Select-Object DeviceID,
        @{n='SizeGB';e={[math]::Round($_.Size/1GB)}},
        @{n='FreePct';e={[math]::Round($_.FreeSpace/$_.Size*100)}} |
    Where-Object FreePct -lt 15 |
    Sort-Object FreePct

FAQ

What’s the difference between Where-Object Status -eq x and Where-Object { $_.Status -eq 'x' }? The first is short syntax for simple comparisons; the script block form handles anything, including several conditions joined with -and / -or.

What’s next

Next: Variables, loops and conditions.

Practise this topic

Test yourself with free questions on this topic – instant answers and explanations.

Start practice questions

PowerShellPipelineBeginnerReporting

Gopinath Karthikeyan

Gopinath Karthikeyan – Windows Site Reliability Engineer with about 12 years in enterprise Windows Server infrastructure, and founder of WinOps Academy. YouTube · LinkedIn

More in PowerShell