Add Column to CSV Windows PowerShell

后端 未结 5 1517
野的像风
野的像风 2020-11-28 10:52

I have a fairly standard csv file with headers I want to add a new column & set all the rows to the same data.

Original:

column1, column2
1,b
2         


        
5条回答
  •  一个人的身影
    2020-11-28 11:04

    Here's one way to do that using Calculated Properties:

    Import-Csv file.csv | 
    Select-Object *,@{Name='column3';Expression={'setvalue'}} | 
    Export-Csv file.csv -NoTypeInformation
    

    You can find more on calculated properties here: http://technet.microsoft.com/en-us/library/ff730948.aspx.

    In a nutshell, you import the file, pipe the content to the Select-Object cmdlet, select all exiting properties (e.g '*') then add a new one.

提交回复
热议问题