none
Import-CSV cmdlet barfs when csv header row has terminal comma - can this behavior be improved RRS feed

  • Question

  • It took me a while to figure this out, but a csv file having a terminal comma in the header (and thus an extra matching comma in each record) causes the Windows PowerShell cmdlet Import-CSV to issue the following strange message:

    Import-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and ru
    n the operation again.
    At C:\ScratchPad\importCSV.ps1:4 char:11
    + Import-csv <<<<  -path $pathi |
        + CategoryInfo          : InvalidArgument: (:) [Import-Csv], PSArgumentException
        + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ImportCsvCommand

    The header looks like this: "field 1","field 2",...,"fieldn",

    Removing the final comma in each records fixes the problem. I am no expert, but is the error message off-target? What "name" is it referring to? Can a better message be provided for the next poor soul who runs into this? Of course I will get the creator of the  file to fix the problem upstream, but still. . .

    Thanks!


    FredsIT
    Wednesday, July 27, 2011 8:11 PM

Answers

  • Import-csv creates an array of PS Custom objects from a csv file.  The propery names will be taken from the column headings of the .csv file.  The "Name" it's referring to is the name of the property it's trying to create.  It's not happy because it's being asked to create a property with a null name.

    You can file a suggestion for changes in future releases here:

    https://connect.microsoft.com/powershell


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, July 27, 2011 8:33 PM
    Moderator

All replies

  • Well, you can't change the error message easily, but you can do this:

    Import-csv -path $pathi -Header "field 1","field 2",...,"fieldn","Ignore"

    Where "Ignore" is the last, empty field.

    Karl 


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Proposed as answer by jrv Thursday, July 28, 2011 12:06 AM
    Wednesday, July 27, 2011 8:27 PM
  • Import-csv creates an array of PS Custom objects from a csv file.  The propery names will be taken from the column headings of the .csv file.  The "Name" it's referring to is the name of the property it's trying to create.  It's not happy because it's being asked to create a property with a null name.

    You can file a suggestion for changes in future releases here:

    https://connect.microsoft.com/powershell


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Wednesday, July 27, 2011 8:33 PM
    Moderator
  • Karl - good solution to a very aggravating problem.

    I was going to do it differently but your solution is better.

    <amy hand rolled data extractors maje the asame mistake.  The roll out thecolumns and forget to supress the last comma.

     

     


    jv
    Thursday, July 28, 2011 12:08 AM
  • Thanks, Karl. I appreciate that. I had hoped to make an easily customized filter for our in-house use where we do a lot of data smithing. The user would supply the name of the field of interest and the argument, and the job would be done. This complication makes it more laborious for the not-so-techical user. Or the script could figure it all out, which is a nice little puzzle, but for the sake of expediency I chose to work around the problem.

    My work-around is to get-content and then walk the content array, filtering and writing out each line. It's more work up front, and less efficient overall, but can be made general with little user action beyond specifying the field and argument.

    I will keep your approach in mind; it will inevitably be needed.

    Best regards,

    Fred


    FredsIT
    Thursday, July 28, 2011 1:03 AM
  • Fred,

    Thanks for this post.  I was having the same issue with powerCLI and could not find the extra delimiter.  It did not occur to me that the last item had a carriage return after it, so it did not need a ","


    Tony

    Friday, February 22, 2013 2:29 PM