Reputation: 227
Is it possible to import a csv file into powershell, replace the entire header row with a defined set of data then save to csv file out with a new name.
I need to be able to automate this task so it can be used by another software.
Any ideas will be greatly appreciated.
Upvotes: 2
Views: 16926
Reputation: 46710
Sample file contents
1,b,4
8,j,7
8,k,9
To make your own header:
Import-Csv D:\Temp\txt.txt -Header "name","letter","value"
name letter value
---- ------ -----
1 b 4
8 j 7
8 k 9
To skip rows:
Import-Csv D:\Temp\txt.txt -Header "name","letter","value" | select -skip 1
name letter value
---- ------ -----
8 j 7
8 k 9
To tie it all up with an export:
$tempCSV = Import-Csv D:\Temp\txt.txt -Header "name","letter","value" | select -skip 1
$tempCSV | Export-CSV D:\Temp\txt.txt -NoTypeInformation
Upvotes: 5
Reputation: 71
A simple way to handle this can be: $temp = Get-Content ~\<CSVFileName>.csv; $temp[0] = $temp[0].replace($temp[0], "<NewCommaSeperatedHeader>"); Set-Content -Path ~\<ModHeaderCSVFile>.csv -Value $temp;
Upvotes: 1