0

i have a large number of CSV files to merge together with each CSV containing one column. I want each csv file to be a new column in the output file. So far i've got this which works for three files but i can't think of how to scale this up to cycle through a large number of files. Any Ideas?

$csv1 = @(gc "C:\xxx\first.csv")
$csv2 = @(gc "C:\xxx\Second.csv")
$csv3 = @(gc "C:\xxx\Third.csv")
$csv4 = @()
for ($i=0; $i -lt $csv1.Count; $i++) {
$csv4 += $csv1[$i] + ', ' + $csv2[$i] + ', ' + $csv3[$i]
}
$csv4 | Out-File "C:\xxx\merged.csv" -encoding default
JoeP
  • 3
  • 2
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `$csv4 = $csv1 |Join $csv2 |Join $csv3` – iRon Nov 10 '22 at 13:02
  • Maybe you should rephrase the question so it becomes clearer that you problem is not merging two csv files, but iterating over a dynamic set of files to merge them. – rob2universe Nov 15 '22 at 03:13

0 Answers0