Import-CSV Header not first line

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

  • Author
    Posts
  • #9248
    Profile photo of Martin Krämer
    Martin Krämer
    Participant

    Hi,
    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

    mk.maddin

  • #9253
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Zhijie Jerry Shi
    Zhijie Jerry Shi
    Participant

    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.