Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created August 26, 2025 18:10
Show Gist options
  • Save steviecoaster/f0ec0a554d72b0dbd0baa62e5b7efd14 to your computer and use it in GitHub Desktop.
Save steviecoaster/f0ec0a554d72b0dbd0baa62e5b7efd14 to your computer and use it in GitHub Desktop.
Quickly merge multiple csv files into a single csv file
function Merge-Csv {
<#
.SYNOPSIS
Merge the data from multiple csv files into one
.DESCRIPTION
Merge data from multiple csv files into one. Data from each file should be the same shape
.PARAMETER CsvFile
The path to two or more csv files to combine
.PARAMETER OutFile
The filename of the combined csv to created
.PARAMETER NoHeader
When used, no header row with column names is provided to the output
.PARAMETER NoClobber
Use this parameter so that Export-Csv does not overwrite an existing file. By default, if the file exists it will be overwritten without warning
.PARAMETER NoTypeInformation
Remove the #TYPE information header from the output. default in PowerShell 6+, included for compatibility
.EXAMPLE
Merge-Csv -CsvFile @('data1.csv', 'data2.csv', 'data3.csv') -OutFile 'combined_data.csv'
Merges three CSV files (data1.csv, data2.csv, and data3.csv) into a single file called combined_data.csv
.EXAMPLE
Merge-Csv -CsvFile @('sales_q1.csv', 'sales_q2.csv') -OutFile 'sales_combined.csv' -NoHeader
Merges two CSV files without including column headers in the output file
.EXAMPLE
Merge-Csv -CsvFile @('report1.csv', 'report2.csv') -OutFile 'final_report.csv' -NoClobber
Merges CSV files but prevents overwriting if final_report.csv already exists
.EXAMPLE
Merge-Csv -CsvFile @('log1.csv', 'log2.csv', 'log3.csv') -OutFile 'all_logs.csv' -NoTypeInformation -NoClobber
Merges three log files, removes PowerShell type information from output, and prevents overwriting existing files
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[ValidateScript({ Test-Path $_ })]
[String[]]
$CsvFile,
[Parameter(Mandatory)]
[String]
$OutFile,
[Parameter()]
[Switch]
$NoHeader,
[Parameter()]
[Switch]
$NoClobber,
[Parameter()]
[Switch]
$NoTypeInformation
)
process {
$csvData = Import-Csv $CsvFile
$csvData | Export-Csv -NoClobber:$NoClobber -NoTypeInformation:$NoTypeInformation -NoHeader:$NoHeader -Path $OutFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment