Script Dumpster: Merge-CSV
Contents
The Problem
A customer required the inventory of product keys for various products on all network systems. I had used a tool which scanned all machines and nicely produced a .csv file for each machine it had scanned.
Ideally I wanted to have a single .csv file which I could then filter against, either using Excel or PowerShell.
The Function
Check the examples notes on how to use the function.
function Merge-CSV{
<#
.SYNOPSIS
Easily merge multiple .csv files into one
.DESCRIPTION
Easily merge multiple .csv files into one using splatting to a temporary Output variable
and adding the content of each .csv file to this Output variable.
Once each .csv file has been processed, the Output variable is written to a new .csv file.
.PARAMETER SourceFiles
Enter paths to multiple .csv files which you would like to merge
.PARAMETER OutputFile
Enter filename/path for the merged file
.NOTES
Name: Merge-CSV.ps1
author: Robert Prüst
DateCreated: 12-06-2015
.EXAMPLE
$Licenses = Get-ChildItem -Path c:\Licenses\Export -Filter *.csv
Merge-CSV -SourceFiles $Licenses -OutputFile c:\Licenses\Overview.csv
#>
[Cmdletbinding()]
param (
[array]$SourceFiles,
[string]$OutputFile
)
$Output = @()
foreach ($File in $SourceFiles){
$TempFile = Import-Csv $File.FullName
$Output += $TempFile
}
$Output | Export-CSV $OutputFile -NoTypeInformation
}
Happy scripting! 🙂