- 1 year ago
PowerShell - Merge CSVs
Comment
1 / 1
To merge multiple CSV files using PowerShell, you can use the Import-Csv
and Export-Csv
cmdlets. Here's an example script:
powershell$csvFiles = Get-ChildItem -Path "C:\Path\to\csv\files" -Filter "*.csv"
$outputFile = "C:\Path\to\output\merged.csv"
$mergedData = @()
foreach ($file in $csvFiles) {
$data = Import-Csv -Path $file.FullName
$mergedData += $data
}
$mergedData | Export-Csv -Path $outputFile -NoTypeInformation
In this example:
- Set the
$csvFiles
variable to the collection of CSV files you want to merge. Modify the path and filter (-Filter "*.csv"
) to match your file locations and naming conventions. - Specify the
$outputFile
variable with the desired path and filename for the merged CSV file. - Create an empty array
$mergedData
to store the merged data from all the CSV files. - Use a
foreach
loop to iterate through each CSV file. Import the data from each file usingImport-Csv
and append it to the$mergedData
array. - Finally, export the merged data to a new CSV file using
Export-Csv
. The-NoTypeInformation
parameter is used to exclude the type information from being written to the output file.
Make sure to update the file paths and adjust the script according to your specific requirements.