Import-Csv -header $variable

Import-Csv -header $variable

2013-07-17
/ / /

I ran into an issue with Import-CSV where I was trying to pass a variable to the -header and it was failing pretty horribly, creating a single field instead of multiple fields.

I created a script to generate a CSV from Citrix WebInterface .conf files to compare the various web interfaces we have so that when we migrate users from the older interface to the newer interfaces we can properly communicate to them the changes they will experience.  In the course of developing this script I wanted to do a “dir *.conf” command and use that output as the header in the CSV.  Here is what I did originally:

This failed.  Our $header variable became a single header in the CSV.  The help for “Import-CSV” says the -header should have a string value.  I tried $header.ToString() but it didn’t work either.  I found you need to do the following:

This makes an array then adds the appropriate values to the $header variable and the import-csv now has multiple columns.

Hurray!

Post a Comment

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.