Working with Data: CSV, Filtering & JSON

Import-Csv, Where-Object, Group-Object, Measure-Object and ConvertTo-Json.

PowerShell shines with structured data. Import-Csv turns each row into an object whose properties are the column names, and the same handful of cmdlets then filter, group, measure and export the data.

Analysing the test results

Import-Csv results.csv gives six objects with properties test, board, status and duration_s.

Where-Object status -eq "fail" filters by property; Select-Object chooses columns; Format-Table prints them.

Group-Object board, status counts each combination.

Measure-Object computes sum, average and maximum of a property.

ConvertTo-Json turns objects into JSON, ready to send to a web API.

Notes

$rows | Format-Table | Export-Csv out.csv $rows | Export-Csv out.csv -NoTypeInformation

Format-Table turns objects into formatting instructions; anything after it receives those, not your data.

How do you get the average duration of passed tests?

Import-Csv results.csv | Where-Object status -eq pass | Measure-Object duration_s -Average