Powershell – Merge CSV files & Insert file name as a column

Scenario:

The details of application installed on different machines need to be collected through PowerShell script in CSV file, named as computername.csv. Once the details collected, all CSV files need to be merged. While merging the file, the file name (computer name) need to be added as an additional column in merged CSV file.

This script has been created for above scenario. However this can be used to combine multiple CSV files into a single CSV file irrespective of what details you have in your files.

Get Software details:

The following PowerShell script has been used to export the list of installed software from different computers.

$paths = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',        'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'

Get-ItemProperty $paths | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Export-csv -path "$env:computername.csv"

Powershell script to export installed software details in CSV file

Merge CSV files:

The following PowerShell script can be used to merge all CSV files in one. This script will insert one additional column at end with source CSV file name as cell value. Since the above script used the computer name as file name, the resulted column will have the computer name. You can quickly remove .CSV extension in Microsoft Excel using Find & Replace.

$sourcefolder = "C:\CSVfiles"
$sourcefiles = Get-ChildItem -Path $sourcefolder -Filter *.csv

$SourceFiles |
ForEach-Object {
   # $fileName = $_
    $output = Import-Csv -Path $_.FullName |
   
    Add-Member -MemberType NoteProperty -Name 'Filename' -Value $_.Name -Passthru
    $combinedoutput +=  $output
} 

 $combinedoutput | Export-Csv "$sourcefolder\CombinedSoftwareList.csv" -NoTypeInformation

Write-Host "Data merged to single CSV to $sourcefolder\CombinedSoftwareList.csv" -ForegroundColor Green

Related Posts:

Subscribe to Techuisitive Newsletter

Be the first to know about our new blog posts. Get our newsletters directly in your inbox and stay up to date about Modern Desktop Management technologies & news.

Scroll to Top