Search code examples
powershellexport-csv

In trying to rename columns on a csv file, the data is wiped out of the second and third columns


process that fails to get the desired result:

Import-Csv -Path C:\Data\Out2.csv | select A, G, H |Export-Csv C:\Data\Out3.csv -NoTypeInformation
Import-Csv C:\Data\Out3.csv |
 Select-Object @{n='Server Name';e={$_.A}} , @{n='Command';e={$_.B}}, @{n='Path';e={$_.C}} |
     Export-Csv C:\Data\Out4.csv -NoTypeinformation

Solution

  • From your Select only columns A,G,H remain in out3.csv so how do you expect to name the 2nd B and the 3rd C?

    This could work:

    Import-Csv -Path C:\Data\Out2.csv | select A, G, H |Export-Csv C:\Data\Out3.csv -NoTypeInformation
    Import-Csv C:\Data\Out3.csv |
     Select-Object @{n='Server Name';e={$_.A}} , 
                   @{n='Command';e={$_.G}}, 
                   @{n='Path';e={$_.H}} |
         Export-Csv C:\Data\Out4.csv -NoTypeinformation
    

    Was focused to much on the obvious error, the first select,export and re-import and so the intermediary file out3.csv aren't neccessary:

    Import-Csv -Path C:\Data\Out2.csv | 
     Select-Object @{n='Server Name';e={$_.A}} , 
                   @{n='Command';e={$_.G}}, 
                   @{n='Path';e={$_.H}} |
         Export-Csv C:\Data\Out4.csv -NoTypeinformation