Last active
April 5, 2022 07:23
-
-
Save codbugs/76ffe0b0fb8b2d4079e997df89f326b1 to your computer and use it in GitHub Desktop.
This PowerShell script merge several CSV files in just one. All CSV files must have the same headers and in the same order to be effective.
This file contains 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
param( | |
[Parameter(Mandatory=$true)] | |
[String] $Location, | |
[Parameter(Mandatory=$true)] | |
[String] $NameFilter, | |
[Parameter(Mandatory=$true)] | |
[String] $OutputFile | |
) | |
If(Test-Path $OutputFile) { | |
Remove-Item $OutputFile -Force | |
} | |
# Writing headers obtained from the first file found | |
Get-ChildItem -Path $Location -Filter "$NameFilter.csv" | |
| Select -First 1 | |
| Get-Content | |
| Select -First 1 | |
| Out-File $OutputFile -Append | |
# Writing content for each file | |
Get-ChildItem -Path $Location -Filter "$NameFilter.csv" | |
| Foreach { | |
$_ | |
| Get-Content | |
| Select -Skip 1 | |
| Out-File $OutputFile -Append | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment