PowerShell for Windows Admins

Jul 30 2013   12:42PM GMT

off with his head

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: