L3. Filtering, Exporting, and Working with CSV and JSON
Turn command output into useful files. Learn Where-Object, calculated properties, Export-Csv, Import-Csv, ConvertTo-Json, and ConvertFrom-Json for real automation workflows.
Filtering Correctly
Filtering is where PowerShell becomes practical. Start with the most specific command possible, then use Where-Object when the command does not have a built-in filter.
Get-Service | Where-Object Status -eq 'Running'
Get-ChildItem -Path . -File | Where-Object Length -gt 1MBWhen a cmdlet has a native filter, prefer it. For example, Get-ChildItem -Filter *.log lets the provider filter earlier than Where-Object Name -like '*.log'.
Export Without Losing Structure
Get-Process |
Sort-Object CPU -Descending |
Select-Object -First 20 Name, Id, CPU, WorkingSet |
Export-Csv -Path .\top-processes.csv -NoTypeInformationImport-Csv .\top-processes.csv | Sort-Object CPU -Descending
CSV is excellent for tables. JSON is better for nested objects and API payloads.
$payload = [pscustomobject]@{
computer = $env:COMPUTERNAME
time = (Get-Date).ToString('o')
services = Get-Service | Where-Object Status -eq 'Running' | Select-Object -First 5 Name, Status
}$payload | ConvertTo-Json -Depth 4 | Set-Content .\snapshot.json
Get-Content .\snapshot.json -Raw | ConvertFrom-Json
Calculated Properties
Calculated properties let you rename or transform data during selection:
Get-Process |
Select-Object Name, Id, @{ Name = 'MemoryMB'; Expression = { [math]::Round($_.WorkingSet64 / 1MB, 2) } }
Lab
- Export running services to CSV.
- Import that CSV and sort by service name.
- Create a JSON file with your hostname, current date, and PowerShell version.
- Add one calculated property to convert bytes to MB.
Sources
- Microsoft Learn: Everything about hashtables
- ✓Prefer native provider filters when available, then use Where-Object for object-level filtering
- ✓Export-Csv is for table-shaped data; JSON is better for nested data and APIs
- ✓Use -NoTypeInformation for clean CSV output in modern scripts
- ✓Use ConvertTo-Json -Depth when serializing nested objects
- ✓Calculated properties transform and rename data without losing object structure
1. Which format is usually better for nested API-style data?
2. What does a calculated property let you do?
Recommended: Pluralsight
This free course covers the theory. Pluralsight adds structured video courses, hands-on Azure labs, and timed practice exams to make it stick before exam day.