PowerShell: How to Merge Multiple CSV Files into One


You can use the following syntax in PowerShell to merge multiple CSV files into one:

Get-ChildItem -path $path -Filter *.csv |
Select-Object -ExpandProperty FullName |
Import-Csv |
Export-Csv all_data.csv -NoTypeInformation -Append

This particular example will merge all CSV files from the folder specified by the $path variable and output them to a single CSV file named all_data.csv.

The following example shows how to use this syntax in practice.

Example: How to Merge Multiple CSV Files into One Using PowerShell

Suppose that we have three CSV files located at the following paths that we would like to merge into one CSV file:

  • c:\users\bobbi\data3\starters.csv
  • c:\users\bobbi\data3\backups.csv
  • c:\users\bobbi\data3\reserves.csv

We can use the Get-Content cmdlet to view the contents of each of these CSV files:

We can see that each of the three files contain information about various basketball players.

Note that each of the CSV files have the same column names.

We can use the following syntax to merge each of these CSV files into one file named all_data.csv:

$path = "c:\users\bobbi\data3"

Get-ChildItem -path $path -Filter *.csv |
Select-Object -ExpandProperty FullName |
Import-Csv |
Export-Csv all_data.csv -NoTypeInformation -Append

The following screenshot shows how to use this syntax in practice:

PowerShell merge multiple CSV files into one

We have successfully merged all three CSV files into one CSV file.

Notice that the CSV file contains all of the rows from each of the files and notice that the header row only occurs once at the top of the file.

Note: When using Export-Csv we included -NoTypeInformation to remove the #TYPE information that would otherwise appear in the header of the CSV file.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Add Column to CSV
PowerShell: How to Use Export-Csv with No Headers
PowerShell: How to Use Export-Csv and Specify Column Order

Leave a Reply