Manage Array: merge the rows

Hello to all.
Sorry to disturb you but I can’t do a thing with Powershell. I ask for your valuable help.

I have this file (csv)
email,password
me@domain.org,Passw0rd
you@domain.com,password1
you@domain.com,password2
you@domain.com,password3
they@domain.com,passwordxxx
it@domain.org,passwordyyy

This file should look like this:
email,password
me@domain.org,Passw0rd
you@domain.com,password1 TAB password2 TAB password3
they@domain.com,passwordxxx
it@domain.org,passwordyyy

The logic is this:
The original file contains several lines that have the same mail (you@domain.com)
I would like these lines to become one and the password field to be the ‘sum’ of all the passwords that correspond to the email.

I hope I have explained quite well

Thank you all for your attention.
Happy holidays
Andrea

Apologies, I deleted my last post because my mush brain got the code mixed up, this should do ya. Basically take the CSV, loop through it, have a condition which states if the email already exists in the collection of objects, don’t add it just update the password, if it does not exist add it into the collection.

$csv= Import-Csv .\testbook.csv
$objectCollection = @()
Foreach($x in $csv){
if ($objectCollection.email -contains $x.email){
## I might not have the syntax correct to format this into a string
    
($objectCollection | where{$_.email -eq $x.email}).password += "`t $($x.password)"

}else{
	$objectCollection += new-object PSCustomobject -property @{
             email = $x.email
             password=$x.password}
}
}
$objectCollection | export-csv .\somwhere.csv

There might be a more efficient way of doing it but that is my first (actually second) thought on how to do it

You can use

to transform your CSV file to the desired format.

That looks to be a much better way of doing it :grin:

… could look something like this:

$CSVInput = @'
email,password
me@domain.org,Passw0rd
you@domain.com,password1
you@domain.com,password2
you@domain.com,password3
they@domain.com,passwordxxx
it@domain.org,passwordyyy
'@ | ConvertFrom-Csv

$CSVInput | 
    Group-Object -Property email |
        Select-Object -Property @{Name = 'email'; Expression = {$_.Name}},
            @{Name = 'password'; Expression = {$_.Group.password -join ' '}}

Oh I like that, I personally have a tendency to not use Select-Object to its full potential and rely on putting everything into objects. Yet that looks a lot neater.

Of course you could use a [PsCustomObject] but the result would look the same and for only 2 properties I don’t mind to do it with calculated properties.

Dear Alex,
I don’t know how to thank you !!! :grinning:
Your code is perfect !
Occasionally I have to do some scripts in Powershell but as you may have guessed I’m not very good!

You helped me a lot.
Thanks again for your super valuable help.
Have a good life!
Andrea

Thank you Olaf,
I just saw your sharp code.
Brilliant !

When I see your codes I realize how ignorant I am!

When I ask this community for help, I always get very useful answers.
Thank you so much.
Hello and good life
Andrea