Reputation: 147
I have been trying to merge a few CSV files into just one file using PowerShell. I have done this plenty of times using batch files without an issue, but I have not been able to do it using PowerShell.
This is the code I am using:
Get-ChildItem C:\Users\kz280\Desktop\Script_Development\Test_Logs\*.csv |
ForEach-Object {Import-Csv $_} |
Export-Csv -NoTypeInformation C:\Users\kz280\Desktop\Script_Development\Test_Logs\MergedCsvFiles.csv
I have also tried 4 or 5 more similar/proven codes I found in Stack Overflow with no success. I just started playing with PowerShell so I am unsure if I am doing something wrong. I have already created/ran a few more scripts without issues.
The error I am getting is that the script runs indefinitely. Besides that, it is only merging three columns. The rest of the 50 columns are not showing up in the final CSV files.
How can I combine all the source files into a single file?
Upvotes: 3
Views: 8295
Reputation: 46730
I feel you are having at least one of two possible issues. What Bill_Stewart said about your output file also being picked up as an input is true. You need to break the pipe after the first Get-ChildItem
or pick a different output directory.
$files = Get-ChildItem C:\Users\kz280\Desktop\Script_Development\Test_Logs\*.csv
$files | ForEach-Object {Import-Csv $_} |
Export-Csv -NoTypeInformation C:\Users\kz280\Desktop\Script_Development\Test_Logs\MergedCsvFiles.csv
This way the output file is not processed as an input file.
I feel like the files you are trying to merge are not of the exact same structure. So I offer a simple method of merging the files if that is the case
$files = Get-ChildItem C:\Users\kz280\Desktop\Script_Development\Test_Logs\*.csv
Get-Content $files | Set-Content C:\Users\kz280\Desktop\Script_Development\Test_Logs\MergedCsvFiles.csv
This is very crude and will repeat columns. This is what you might want though. Update your question with more details if we are on the wrong track.
Upvotes: 1