Contents

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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! 🙂