Arturka1
Arturka1

Reputation: 65

add new header column to .csv file using powershell

I'm trying to add a new header column to a .csv file with using powershell. When this is done the next step is to fill all data columns with the same entry it should look like this:

Before:

Name;Kind;Type1;Type2;Type3;
Function1;Method;something1;something1;something1;
Function2;Method;something1;something1;something1;

After:

Name;Kind;Type1;Type2;Type3;Version
Function1;Method;something1;something1;something1;1
Function2;Method;something1;something1;something1;1

So because of the file has ~35 columns and ~30000 rows i need to loop through But i dont know how to get to the end of a line.

Upvotes: 0

Views: 1959

Answers (1)

arco444
arco444

Reputation: 22821

You can do this:

$inFile = "C:\path\to\your\file.csv"
$outFile = "C:\path\to\your\modifications.csv"
$v = Import-Csv -Delimiter ';' $inFile
$v | % { $_ | Add-Member -NotePropertyName "Version" -NotePropertyValue 1 }
$v | ConvertTo-Csv -Delimiter ';' -NoTypeInformation | Out-File $outFile

Import the csv file to a powershell object, then iterate over all the lines and use the Add-Member cmdlet to add an extra property.

Then convert the object back to csv format and save to a new file. You can of course overwrite the original file with this, but be careful the information is correct before you do it.

Upvotes: 2

Related Questions