Append Data to a CSV File in PowerShell

Rohan Timalsina Jun 27, 2022
Append Data to a CSV File in PowerShell

A Comma-Separated Values (CSV) file is a delimited text file that stores data in a tabular format. Each value is separated by the delimiter , or other characters.

The Export-Csv cmdlet in PowerShell creates a series of CSV strings from given objects and saves them into a file. While working with CSV files on PowerShell, there are times when you will need to append a CSV file.

This tutorial will teach you to append data to a CSV file using PowerShell.

Use -Append Parameter to Append Data to a CSV File in PowerShell

The following example creates a new CSV file command.csv with the Command objects in the directory C:\New.

Get-Command | Export-CSV -Path C:\New\command.csv

The Get-Command gets all commands that are installed on the computer. The resulting output is piped to the Export-Csv cmdlet, which converts the output objects to a series of CSV strings.

The -Path parameter specifies the location to save the CSV file.

Now, go to the saved directory and open the CSV file with MS-Excel or a similar program. The output should look as shown below.

create a csv file in powershell

When you try to add new objects to a CSV file, it will replace the old content. The -Append parameter allows you to export data to a CSV file without replacing any content.

The following example appends the output of the Get-Alias cmdlet to the C:\New\command.csv file.

Get-Alias | Export-CSV -Path C:\New\command.csv -Append

The Get-Alias cmdlet gets the aliases in the current session. The output objects are piped to the Export-Csv cmdlet to save in a command.csv file.

The -Append parameter adds data to the end of a file.

Output:

append data to a csv file using powershell

That’s it. Now you should know how to append data to CSV with PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell CSV