This could be helpful when you need to update existing CSV file and you don’t have Excel in hand. You can do it in Notepad for sure, but it is easier with PowerShell.
$CSVFile = import-csv 'C:\temp\SourceFile.csv'
$Counter = $CSVFile.count
foreach($Line in $CSVFile)
{
$NewColumnValue = 'This is the new column value'
$Line | Add-Member -NotePropertyName NewColumnName -NotePropertyValue $NewColumnValue
$Counter = $Counter -1
Write-Host $Counter
}
$CSVFile | Export-CSV 'c:\temp\ResultFile.csv'
If you are curious what else you can do with Excel spreadsheets in PowerShell – check out the ImportExcel PowerShell module
More articles about PowerShell available.
Perfect. Thanks
You are welcome!
Exactly what I needed thanks, but anyone using this just note there is a type in the first variable name, “CVSFile” should be “CSVFile”
Thanks for the feedback. Fixed now.
Coming to large data, this solution will not good
Thanks for that. I don’t know how large should be the CSV file, worked well for me and for others.
Can you share more details about the issues and maybe share a better method with me?
It worked for me for a 50MB file with over 130,000 records. I guess it would limited by the amount of memory in your system to hold the input file in $CSVFile.
Hi
Is there something like Remove-Member option to remove a column or rename a column?
Hi Muhammad,
Thank you for the question.
No, there is no such function as Remove-Member. I think the best option would be to ger through all the records and write to a new file the required only.