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.
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.
-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
Get-Command | Export-CSV -Path C:\New\command.csv
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.
-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.
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
Get-Alias | Export-CSV -Path C:\New\command.csv -Append
Get-Alias cmdlet gets the aliases in the current session. The output objects are piped to the
Export-Csv cmdlet to save in a
-Append parameter adds data to the end of a file.
That’s it. Now you should know how to append data to CSV with PowerShell.
Related Article - PowerShell CSV
- PowerShell Extract a Column From a CSV File and Store It in a Variable
- Import Text File and Format and Export It to CSV in PowerShell
- Combine CSV Files in PowerShell
- Import CSV Files Into Array in PowerShell
- Convert XML to CSV File in PowerShell