April 27, 2023

How to merge CSVs with PowerShell

This handy one liner comes in handy from time to time:

(Get-ChildItem *.csv -Exclude *merged*.csv | `
Select-Object -ExpandProperty fullname | Import-CSV) | `
Export-Csv .\merged.csv -NoTypeInformation

Demo

To see it in action use the below to create a directory called foo. Then generate some demo CSV files with a custom function (see hash table to CSV).

mkdir foo
cd .\foo
function ToCSV {
    param (
        [string] $Filename,
        [string] $key,
        [string] $value
    )

    if(-not (Test-Path $Filename)){
        $hashtable = @{}
        $hashtable.Add($key, $value)
        $hashtable.GetEnumerator() | Select-Object -Property Key, Value | `
        export-csv -NoTypeInformation -Path $Filename
    }
}

# create CSVs
ToCSV -Filename foo.csv -key a -value 1
ToCSV -Filename bar.csv -key b -value 2
ToCSV -Filename foobar.csv -key c -value 3

Then call the code to merge the files:

(Get-ChildItem *.csv -Exclude *merged*.csv | Select-Object -ExpandProperty fullname| Import-CSV) | Export-Csv .\merged.csv -NoTypeInformation