Merging multiple CSV Files with Powershell

Powershell is a great tool for managing CSV files, specially with the cmdlets Export-Csv and Import-Csv. Let´s use it to merge multiples several CSV files. The process is:

  • Get the list of CSV files
  • Import the content of every CSV file into the same variable
  • Export the content of the variable to a final CSV file

GET THE LIST OF CSV FILES

First we get the list of CSV files, which are located in a folder using the Get-ChildItem cmdlet:

$csvfiles=Get-ChildItem -Path C:CSVFiles*

If there are other files with other extensions we can use wildcards like this:

$csvfiles=Get-ChildItem -Path C:CSVFiles* -Include *.csv

Or, if there are more CSV files than you are intested on you can still use wildcards to filter by title for example:

$csvfiles=Get-ChildItem -Path C:CSVFiles* -Include *<word_of_interest>*.csv

IMPORT THE CONTENT TO POWERSHELL

Once we got the list of CSV files to merge, we process each file importing the content of each file to a $content variable

foreach ($file in $csvfiles) {

$content += Import-Csv -Path $file;

}

EXPORT THE CONTENT

Last step is export the content of the $content variable to a CSV file using the Export-Csv cmdlet

$content | Export-Csv -Path C:CSVFilesMerged.csv

And that’s all…

Note: this method is very simple but we need to have the same structure in the CSV files (same columns), since the declaration of columns is found in the first line of a CSV.

Hope it helps!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s