none
powershell: How to change the header name in a imported CSV file RRS feed

  • Question

  • HI All,

    I have a csv file in which I want to change the headers names in powershell.

    oldnames are name,id

    newnames I want to give are: company and transit respectively.

    Following is what I wrote in script:

               $a = import-csv .\finalexam\employees.csv -header name,id'

                       foreach ($a in $as[1-$as.count-1])  // I used 1 here because I want it to ignore the  exiting headers.

                      { $_.name -eq company, $_.id -eq transit}

    I think this not entirely correct way to do this. Can someone show me how to fix this.

    Regards

    SQL 75


    • Edited by SQL75 Friday, April 10, 2015 7:42 PM
    Friday, April 10, 2015 7:40 PM

Answers

  • $a = Import-Csv .\finalexam\employees.csv -Header "company","transit" | select -skip 1
    • Marked as answer by SQL75 Friday, April 10, 2015 8:05 PM
    Friday, April 10, 2015 7:56 PM

All replies

  • If you're not an experienced PowerShell programmer, I recommend editing the file in a text editor and changing the name that way.


    -- Bill Stewart [Bill_Stewart]

    Friday, April 10, 2015 7:47 PM
    Moderator
  • $a = Import-Csv .\finalexam\employees.csv -Header "company","transit" | select -skip 1
    • Marked as answer by SQL75 Friday, April 10, 2015 8:05 PM
    Friday, April 10, 2015 7:56 PM