Skip to content

Instantly share code, notes, and snippets.

@codbugs
Last active April 5, 2022 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codbugs/76ffe0b0fb8b2d4079e997df89f326b1 to your computer and use it in GitHub Desktop.
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.
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