Categories

off with his head

CSV files are great for passing information around and are very easy to use in your PowerShell scripts. Sometimes, your CSV may have a problem – for instance it doesn’t have a header row. At that point the data becomes a bit difficult to use:

£ Import-Csv .\htest.csv | Format-Table -AutoSize

2  Red   23.8 47.6
-  ---   ---- ----
7  Blue  24.0 168.0
15 Green 2.5  37.5

Luckily there is an answer.  Create your own headers:

£ Import-Csv .\htest.csv -Header "Number", "Colour", "Price", "Total" | Format-Table -AutoSize

Number Colour Price Total
------ ------ ----- -----
2      Red    23.8  47.6
7      Blue   24.0  168.0
15     Green  2.5   37.5

Makes much more sense now.

You can also override existing headers

£ Import-Csv .\htest.csv -Header "Number", "Colour", "Price", "Total"  |
>> select -Skip 1 | Format-Table -AutoSize
>>

Number Colour Price Total
------ ------ ----- -----
2      Red    23.8  47.6
7      Blue   24.0  168.0
15     Green  2.5   37.5

The select call is used to skip the old header row.

If you run queries in SQL Studio Management Studio and export the results to a CSV file you will get a file without headers. This technique comes in useful at that point.

Leave a Reply