Is it possible to read the contents of .csv and add another column data for every record ?.

Example : The input CSV will have the following contents

Row 1 : C1 C2 C3 C4 <C5>

Is it feasible to set the C5 data with a fixed value using Windows scripting ?

Then the .csv file should be saved also with the added column.


From your description i assume you have a csv file to which you want to add an extra column and for every row add a value to thos column.

Assuming the in-file is input.csv and the output is output.csv this line in Powershell would do the trick. It takes a file input.csv in your current directory and outputs a file output.csv with an extra column HEADER and constant value VALUE.

I have broken it to several lines for ease of reading...

 Get-Content .\input.csv |
 ForEach-Object{ If($n -eq 0){ $n=1; $_ + ",HEADER" } Else { $_+",VALUE"} } |
 Out-File output.csv

UPDATE: To make a batfile of the command, you should prefix it with "powershell -Command".

In summary: you want the bat-file to contain the following line:

powershell.exe -Command "$n=0; Get-Content .\input.csv | ForEach-Object{ If($n -eq 0){ $n=1; $_ + ',HEADER' } Else { $_+',VALUE'} } | Out-File output.csv"

  • Thanks. I am new to this powershell. Can i put the above statements in a batch file and call it as such ? – UnderDog Oct 2 '14 at 12:50
powershell $cn=gc cn.csv;$cl=gc c1.csv;$i=0;$cn^|%{$cn[$i]+','+$cl[$i];++$i}>result.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.