Import-CSV Header not first line

This topic contains 2 replies, has 3 voices, and was last updated by  Zhijie Jerry Shi 1 year, 10 months ago.

  • Author
  • #9248

    Martin Krämer

    I am having a CSV having 2 lines of description and whatever and 3 line is my header.
    How can I tell the Import-CSV command to use the 3 line instead of line 1 for header?

    Thank you for help


  • #9253

    Dave Wyatt

    Technically, that's not a valid CSV file. However, you should be able to get around this by using Get-Content, Select-Object with the -Skip parameter, and ConvertFrom-Csv instead of Import-Csv:

    $csvPath = 'C:\Temp\Whatever.csv'
    $csvData = Get-Content -Path $csvPath | Select-Object -Skip 2 | ConvertFrom-Csv
  • #32213

    Zhijie Jerry Shi

    Dave's solutions almost worked. But I ran into problems that line breaks in quotations are not handled correctly. It took me quite some time to realize the problem and find an easy solution to fix it: concatenate lines (using Out-String) before sending them to ConvertFrom-Csv.

    $csvPath = 'C:\Temp\Whatever.csv'
    $csvData = Get-Content -Path $csvPath | Select-Object -Skip 2 | Out-String | ConvertFrom-Csv

    Using a temporary file also works.

You must be logged in to reply to this topic.