Friday, February 24, 2023

Merge-csv in powershell

 This is a powershell script to merge all csv files in a folder.  

This works where you have multiple files with the same structure into a single file.


#Get the header from one of the CSV Files, write it to outfile.tmp
Get-ChildItem *.csv|select -First 1|Get-Content|select -First 1|Out-File -FilePath .\outfile.tmp -Force
#Get the content of each file, excluding the first line and append it to outfile.tmp
Get-ChildItem *.csv|foreach {Get-Content $_|select -Skip 1|Out-File -FilePath .\outfile.tmp -Append}
#rename the file with timestamp to run muliple time
Rename-Item -Path .\outfile.tmp -NewName .\"Output-$(get-date -f yyyyMMddHHMMss).txt"

 # Used code from this blog -

No comments:

Post a Comment