TIL: Export-Csv can skip columns

Was doing something like the following in one of my scripts:

This takes an array of hash tables and export them into a CSV. Unfortunately (and thankfully, coz I almost missed it!) some of the columns were missing in the CSV file. They appeared fine if I skipped the Export-Csv and looked at the output on screen, but in the file columns were missing.

Turns out this is because Export-Csv goes with the “columns” defined by the first element in the array, so anything not in that element is skipped for all the others. See this SO post for an explanation and some examples.

One workaround is to create a list of attributes across all the elements and then do a Select-Object.

One learns… :)