Problem with Import-Csv and headers

This topic contains 2 replies, has 3 voices, and was last updated by  Olaf Soyk 2 weeks ago.

  • Author
    Posts
  • #90991

    Karol Rogowski
    Participant

    Hello
    I'm having a problem with getting right columns using Import-Csv -Header
    My CSV file is

    AAA,BBB,CCC
    aaa1,bbb1,ccc1
    aaa2,bbb2,ccc2

    PS code that I'm using is

    $headers = "BBB", "CCC"
    $files = Import-Csv "C:\Users\karol\Desktop\CSV_TEST\Files.csv" -Header $headers
    $files

    The result are not what it would looks like.

    BBB CCC
    — —
    AAA BBB
    aaa1 bbb1
    aaa2 bbb2

    Any idea what is the issue? THX

  • #90994

    Jon
    Participant

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-5.1

    -Header
    Specifies an alternate column header row for the imported file. The column header determines the names of the properties of the object that Import-Csv creates.

    Enter a comma-separated list of the column headers. Enclose each item in quotation marks (single or double). Do not enclose the header string in quotation marks. If you enter fewer column headers than there are columns, the remaining columns will have no header. If you enter more headers than there are columns, the extra headers are ignored.

    When using the Header parameter, delete the original header row from the CSV file. Otherwise, Import-Csv creates an extra object from the items in the header row.

  • #90997

    Olaf Soyk
    Participant

    Seems like your original CSV file already has headers ... at least AAA and BBB. Either you use the existing headers in CSV file or you specify them with your cmdlet. If I'm not wrong you cannot combine them.

You must be logged in to reply to this topic.