I cannot work out how to rename a column heading in a CSV file with Windows PowerShell. I can add new rows, delete rows, fill, and export, but cannot find out how to change a header value.
I thought I could import the CSV & export with headers specified, but taht doesn't work:
import-csv c:\tmp\test.csv | Select-Object first_name | Export-Csv -header "test" c:\tmp\test1.csv
I basically need to re-format a CSV file, so if I can select the data I want & specify the new headers that would be perfect.
Select a column, and then select Transform > Rename. You can also double-click the column header. Enter the new name.
1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.
You may use Select-Object
with a calculated property to do this:
Import-Csv test.csv |
Select-Object @{ expression={$_.first_name}; label='test' } |
Export-Csv -NoTypeInformation test1.csv
Can't you try something like this?
import-csv 'c:\tmp\test.csv' | select -Property @{name="test";expression={$($_.first_name)}}| Export-Csv c:\tmp\test1.csv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With