PowerShell Import-Csv Error

I ran across the following error while working with a PowerShell script I was developing:

Import-Csv : Cannot process argument because the value of argument "name" is invalid.
Change the value of the "name" argument and run the operation again.

It had me stumped for a while. The Impor-Csv command was failing, but the file name was good (I could cat the files contents to standard out).

Long story short, the error came from having trailing blank columns in my CSV. Import-Csv uses the first row in the CSV as names for the columns (unless you specify otherwise) and when you have blank columns (or at least multiple blank columns) it causes this error as it doesn’t have a valid name for them. The underlying culprit for the error was actually Microsoft Excel. I was exporting a spreadsheet to a CSV and the spreadsheet had previously had some trailing columns. I values to the columns, but apparently Excel decided they should still show up in the export. Fully deleting the columns so they didn’t show up in the CSV resolved the problem.

Leave a comment ?

17 Comments.

  1. Ahh! Thank you! thank you! thank you!!!

  2. Thanks. That pointed me in the right direction

  3. Thanks! I had an extra tab at the end of each line in my file.

  4. Stanley Hopcroft

    Thank you very much for explaining a very unhelpful error message.

  5. This solved my problem, thanks!

  6. he,

    you have to do this!
    this resolve all problem.

    $csv1 = “C:…….csv”
    cat $csv1 | % {$_.replace(“,,”,”")} | out-file c:\………csv

  7. I added the -header command and it resolved this once I knew what the culprit was, so:

    import-csv “filename.csv” -Header (“Col1″,”Col2″,…)

    This resolves the problem above without editing the file. Thanks for pointing me in the right direction.

  8. many thanks , this helped me a lot :-)

  9. :wink: got me out of a bind. thanks a ton!

  10. Many thanks! That helped me a lot! :grin:

  11. Just want to say that over two years since the original post your entry here is still bailing people out (that would be me!)

    10K thanks for your help.

  12. Thanks so much for posting. Save me so much time!

  13. Buen posting

  14. Thank you for this!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>