Merge multiple CVS files into a single CVS file

This simple powershell script will merge multiple CVS files into a single. Run this script from working directory where the CVS files are located:


$CSVFiles = Get-ChildItem -Filter *.csv | Select-Object -ExpandProperty FullName

$OutputFile = "c:\temp\merged.csv"

$Output = @();
foreach($CSV in $CSVFiles) {
    if(Test-Path $CSV) {
        $FileName = [System.IO.Path]::GetFileName($CSV)
        $temp = Import-CSV -Path $CSV | select *, @{Expression={$FileName};Label="FileName"}
        $Output += $temp
    } else {
        Write-Warning "$CSV : No such file found"
    }
}

$Output | Export-Csv -Path $OutputFile -NoTypeInformation