1

I have a csv file where I am trying to export rows into another csv file only where the values in the id column have duplicates.

I have the following csv file...

"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valThree","valFour"
"valFive","valSix"
"valFive","qwreweq"
"valSeven","valEight"

I need the output csv file to look like the following...

"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"

Here is the code I have so far:

$inputCsv = Import-CSV './test.csv' -delimiter ","
#$output = @()
$inputCsv | Group-Object -prop id, blablah | Where-Object {$_.id -gt 1} |
    Select-Object
        #@{n='id';e={$_.Group[0].id}},
                  #@{n='blablah';e={$_.Group[0].blablah}}
    #Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation
#Write-Host $output
#$output | Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation

I've searched multiple how-to's but can't seem to find the write syntax. Can anyone help with this?

1 Answer 1

2

Just group on the ID property and if there is more than 1 count in the group then expand those and export.

$inputCsv = Import-CSV './test.csv' -delimiter ","

$inputCsv |
    Group-Object -Property ID |
        Where-Object count -gt 1 |
            Select-Object -ExpandProperty group |
                Export-Csv output.csv -NoTypeInformation

output.csv will contain

"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! The Select-Object -ExpandProperty group was the magic I was missing.

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.