Import-CSV Header not first line

Welcome Forums General PowerShell Q&A Import-CSV Header not first line

This topic contains 2 replies, has 3 voices, and was last updated by

3 years, 5 months ago.

  • Author
  • #9248

    Topics: 2
    Replies: 5
    Points: 0
    Rank: Member

    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

    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    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

    Topics: 0
    Replies: 1
    Points: 0
    Rank: Member

    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.

The topic ‘Import-CSV Header not first line’ is closed to new replies.