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
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 questionsPowerShellPipelineBeginnerReporting