Bunion
Bunion

Reputation: 461

merge several .csv files into one array

I'm trying to load several .csv files (with the same columns) into the same array from different directories.

$csv1 = Import-Csv "PATH1"
$csv1 = Import-Csv "PATH2"
$csv1 | Export-Csv C:\test.csv

This just outputs the last .csv loaded, what would be the best way to do this?

Upvotes: 0

Views: 507

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

When in doubt, read the documentation. The Import-Csv cmdlet accepts an array of path strings as input, so all you need to do (assuming that all your CSVs have the same fields) is something like this:

$src = 'C:\path\to\input1.csv', 'C:\path\to\input2.csv', ...
$dst = 'C:\path\to\output.csv'

Import-Csv $src | Export-Csv $dst -NoType

If you want an additional column with the path of the source file you need some additional steps, though:

$src | ForEach-Object {
  $path = $_
  Import-Csv $path | Select-Object *,@{n='Path';e={$path}}
} | Export-Csv $dst -NoType

Upvotes: 2

Related Questions