Home > Net >  "Sort-Object email -Unique" and if there is duplicate leave the one with the higher role a
"Sort-Object email -Unique" and if there is duplicate leave the one with the higher role a

Time:09-02

Sort-Object email -Unique if there is duplicate leave the one with the higher role and remove the rest.

I think I am going to have to do something like this instead of the sort-object

$results = $results | foreach-object {

    If ($results.email -contains $_.email) {
        Foreach ($role in $results) {
            <Find my place in the array then replace with the highest role using a switch>
        }
    } 
}

$results = $results Sort-Object email -Unique

This seems like I am being too complicated and the results don't work out. I am here for advice on how to do this. I will expand the middle section of my code if there is not a better way to do this ?

CodePudding user response:

If your goal is to get the highest-roled email where emails can be duplicated, you want to ensure you sort by email, then role, and select out the unique emails:

$results |
    Sort-Object -Property role, email |
    Select-Object -Property email -Unique

This assumes your object shape is:

[pscustomobject]@{
  email = 'string'
  role = 'string'
  type = 'string'
}
  • Related