Skip to content

Instantly share code, notes, and snippets.

@nsna
Last active June 23, 2021 05:00
Show Gist options
  • Select an option

  • Save nsna/264756f1c9f3671e8f915d1cd4afec1c to your computer and use it in GitHub Desktop.

Select an option

Save nsna/264756f1c9f3671e8f915d1cd4afec1c to your computer and use it in GitHub Desktop.
Combine csv files - Powershell
# Load Excel powershell module; install if it doesn't exist
if (Get-Module -ListAvailable -Name ImportExcel) {
Write-Host "Excel module check passes"
}
else {
try {
Install-Module -Name ImportExcel -AllowClobber -Confirm:$False -Force
}
catch [Exception] {
$_.message
exit
}
}
# get all csv files in current folder
$csvFiles = Get-ChildItem .\* -Include *.csv
# combined output filename
$outputName = "output.xlsx"
# loop through each csv file
$csvFiles | Foreach-Object {
# load csv data from each file
$csv = Import-Csv -Path $_.FullName
# save data as new sheet in output spreadsheet
$csv | Export-Excel -workSheetName $_.BaseName -path ".\$outputName"
}
Write-Host "Job done, exported to: $outputName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment