Home > Enterprise >  Merge Multiple CSV files to one with different columns
Merge Multiple CSV files to one with different columns

Time:11-04

Below is the data I have in 2 csv

CSV_1

"Username","UserCreationStatus","GroupAdditionStatus"
"WA92J4063641OAD","Success","Success"

CSV_2

"GroupName","GroupCreationStatus"
"WA92GRP-ADAdminAccount-CAP-OAD","Already exist"

I need to merge them in to single csv file like below

"Username","UserCreationStatus","GroupAdditionStatus","GroupName","GroupCreationStatus"
"WA92J4063641OAD","Success","Success","WA92GRP-ADAdminAccount-CAP-OAD","Already exist"

I tried the below code

Get-ChildItem -Path $RootPath -Filter *.csv | Select-Object * | Import-Csv | Export-Csv $RootPath\merged.csv -NoTypeInformation -Append

But getting below error

Import-Csv : You must specify either the -Path or -LiteralPath parameters, but not both.

Please let me know what is wrong here

CodePudding user response:

You could do simething like this.
It does not work but shows the logic.
Let me know, if you have any questions.

$CSV1 = ".\first.csv"
$CSV2 = ".\second.csv"
$NewCSV = ".\new.csv"

$Data1 = Get-Content -Path $CSV1
$Data2 = Get-Content -Path $CSV2

foreach ($Line in $CSV1)
{
    Add-Content -Value "$($Line),$($CSV2[$index])" -Path $NewCSV
}
  • Related