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

3
  • 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. Commented Nov 3, 2021 at 7: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 Commented Nov 3, 2021 at 7:48
  • 1
    As general solution, using this Join-Object script/Join-Object Module (see also: In Powershell, what's the best way to join two tables into one?): Import -Csv .\First.csv |Join (Import-Csv .\Second.csv) |Export-Csv .\Merged.csv Commented Nov 3, 2021 at 8:05

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. added the index part and it is working fine. Thanks @Patrick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.