Last active
June 23, 2021 05:00
-
-
Save nsna/264756f1c9f3671e8f915d1cd4afec1c to your computer and use it in GitHub Desktop.
Combine csv files - Powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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