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
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 !!!
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