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

 
Participant
3 years ago.

  • Author
    Posts
  • #9248

    Participant
    Points: 0
    Rank: Member

    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

    Member
    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

    Participant
    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.