How to append data into a CSV file using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To append the data into CSV file you need to use –Append parameter while exporting to the CSV file.

In the below example, we have created a CSV file


$outfile = "C:\temp\Outfile.csv"
$newcsv = {} | Select "EMP_Name","EMP_ID","CITY" | Export-Csv $outfile
$csvfile = Import-Csv $outfile
$csvfile.Emp_Name = "Charles"
$csvfile.EMP_ID = "2000"
$csvfile.CITY = "New York"
$csvfile | Export-CSV
$outfile Import-Csv $outfile

Now we need to append the below data to the existing file. So first we will import the csv file into a variable called $csvfile

$csvfile = Import-Csv $outfile
$csvfile.Emp_Name = "James"
$csvfile.EMP_ID = "2500"
$csvfile.CITY = "Scotland"

Once data is inserted into a variable, we will append data with Append parameter.

$csvfile | Export-CSV $outfile –Append

Check the output:

Import-Csv $outfile


EMP_Name    EMP_ID     CITY
--------    ------     ----
Charles     2000       New York
James       2500       Scotland
Published on 26-Mar-2020 11:56:40