L3. Filtering, Exporting, and Working with CSV and JSON
Course outlineLesson 3 of 10
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 -NoTypeInformation
Import-Csv .\top-processes.csv |
Sort-Object { if ($_.CPU) { [double]::Parse($_.CPU, [cultureinfo]::InvariantCulture) } else { 0 } } -DescendingImport-Csv returns string-valued properties, so cast numeric columns before numeric sorting or arithmetic. Keep objects in the pipeline until export; Format-Table emits formatting instructions rather than the original data.
CSV is excellent for tables. JSON is better for nested objects and API payloads. The following service example requires Windows: Get-Service is available only on Windows. The @() wrapper preserves an array when zero or one services match, and -EnumsAsStrings keeps status names readable.
$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 -EnumsAsStrings | 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.
References
- Where-Object filters
- Select-Object and calculated properties
- Export-Csv
- Import-Csv
- ConvertTo-Json
- Get-Service platform support
Continue Learning
Build reusable functions- ✓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
- ✓-NoTypeInformation is compatible with older scripts and is already the Export-Csv default in PowerShell 6 and later
- ✓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?