0

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

Empty Coder
  • 589
  • 6
  • 19
  • That will not be possible that "simple". How many lines does the CSV has, and how do you know which line belongs to each other? I guess you have to load both CSV files, do a loop to accumulate the data and export it again. – Patrick Nov 03 '21 at 07:43
  • @Patrick: each csv will have 21 line with 3 and 2 columns respectively. I just want to merge both. rest of the data I have checked and it is fine – Empty Coder Nov 03 '21 at 07:48
  • 1
    As general solution, 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)): `Import -Csv .\First.csv |Join (Import-Csv .\Second.csv) |Export-Csv .\Merged.csv` – iRon Nov 03 '21 at 08:05

1 Answers1

1

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
}
Patrick
  • 2,128
  • 16
  • 24