PowerShell, Office 365, Azure and automation
Adding a new column of data to an existing CSV by PowerShell

Adding a new column of data to an existing CSV by PowerShell

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.

9 Comments

    1. your alt in comments
      Koji Kabuto

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *