Change only the header row of a large csv

Welcome Forums General PowerShell Q&A Change only the header row of a large csv

Viewing 2 reply threads
  • Author
    Posts
    • #180693
      Participant
      Topics: 1
      Replies: 1
      Points: 36
      Rank: Member

      I have a large CSV file with a couple of dozen columns, thousands of row. It's auto-updated daily by one system and another system will auto-import the CSV but the second system expects the headers to be different from what the first system generates. Alas I have no control over either system so the solution is to change the header row of that CSV using PowerShell.

      Given a list of matches, e.g.

      {"Green Fruit" -> "Apples",
      "Large Vehicle" -> "Van",
      "President" -> "Head Honcho"}

      What's the quickest/easiest way to parse the CSV and end up with correct headers?

    • #180785
      Participant
      Topics: 5
      Replies: 287
      Points: 224
      Helping Hand
      Rank: Participant

      Using -Header parameter creates an alternate column header row on top of the original header row. I used -Skip parameter to skip the first row (which now contains your original header list). Now you can export the csv with new headers.

      Import-Csv -Path .\importedfile.csv -Header NewFruit,NewVehicle,NewPresident |
      Select-Object -Skip 1 | Export-Csv -NoTypeInformation -Path .\newfile.csv
      
    • #180962
      Participant
      Topics: 1
      Replies: 1
      Points: 36
      Rank: Member

      That did the trick, spiffy automated correction is now running.

      Thanks for your help!

      Graham

Viewing 2 reply threads
  • The topic ‘Change only the header row of a large csv’ is closed to new replies.