Combining CSV Files With PowerShell

Despite using PowerShell for years I’m still frequently blown away by how simple PowerShell makes certain tasks. Take combining CSV files. These files all have the same header and columns, just different data.

Assuming you’re in the folder with the CSV files, it’s a three step process

  • Get a list a of the CSV files
  • Import the CSV files into a variable
  • Export the variable to a single CSV file

The key to this being incredibly simple is that Import-Csv accepts an array of strings for the Path parameter, meaning it can be fed multiple files at once.

$CSVFiles = Get-ChildItem -Filter "*.csv"
$Data = Import-Csv -Path $CSVFiles
$Data | Export-Csv -Path "Data.csv" -NoTypeInformation

This can all be combined into a single command

Import-Csv -Path (Get-ChildItem -Filter "*.csv") | Export-Csv -Path "Data.csv" -NoTypeInformation