5

The following code results in an Array:

[Array]$Object = [PSCustomObject]@{
    P1 = 'Appel'
    P2 = 'Cherry'
    P3 = 'Appel'
    P4 = 'Banana'    
}
$Object += [PSCustomObject]@{
    P1 = 'Good'
    P2 = 'Bad'
    P3 = 'Good'
    P4 = 'Good'
}
$Object += [PSCustomObject]@{
    P1 = 'Green'
    P2 = 'Red'
    P3 = 'Green'
    P4 = 'Yellow'
}
$Object

This generates:

P1         P2         P3       P4                          
--         --         --       --                          
Appel      Cherry     Appel    Banana                      
Good       Bad        Good     Good                        
Green      Red        Green    Yellow 

I'm trying to figure out how I can have it report the duplicates, in this case the desired outcome would be P1 and P3 as they both have the same info in them:

P1      P3                       
--      --                       
Appel   Appel                    
Good    Good                     
Green   Green 

Because the values are not in the same object it's not as simple as using Group-Object to retrieve them. Any help would be appreciated.

2
  • Something along the lines of if (!(Compare-Object $object.p1 $object.p3)) { echo duplicate }? Commented Dec 7, 2015 at 12:34
  • Yes arco444, that's what I'm playing with now.Compare-Object $Object.P1 $Object.P3 -IncludeEqual -ExcludeDifferent. I only need to figure out how to loop through the collection to have it compare everything. Commented Dec 7, 2015 at 12:35

2 Answers 2

6

You could use Group-Object to find duplicate property values in each object by inspecting the value of each entry in the psobject.Properties property:

PS C:\> $Object |ForEach-Object {
    $_.psobject.Properties | Group-Object { $_.Value } | Format-Table
}

Count Name                      Group
----- ----                      -----
    2 Appel                     {string P1=Appel, string P3=Appel}
    1 Cherry                    {string P2=Cherry}
    1 Banana                    {string P4=Banana}


Count Name                      Group
----- ----                      -----
    3 Good                      {string P1=Good, string P3=Good, string P4=Good}
    1 Bad                       {string P2=Bad}


Count Name                      Group
----- ----                      -----
    2 Green                     {string P1=Green, string P3=Green}
    1 Red                       {string P2=Red}
    1 Yellow                    {string P4=Yellow}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Mathias for putting me on the right track, but I really need to compare all 3 values to see if they all 3 match..
I'm still not entirely sure what you trying to accomplish here. None of the 3 objects in your $Object array have duplicates between them
You are correct, the values are not in the same object. I've updated the OP to be more clear.
Yes, but what about the P4 property then? It has a duplicate value in the second object
I don't care about that, it only interests me if all 3 property values are equal.
4

Finally figured it out:

$Props = $Object | Get-Member | ? MemberType -EQ NoteProperty | Measure-Object | Select-Object -ExpandProperty Count

$Result = for ($a = 1; $a -le $Props; $a++) {
    for ($b = 1; $b -le $Props; $b++) {
        if ($a -ne $b) {
            if (($R = Compare-Object ([String[]]$Object.("P$a")) ([String[]]$Object.("P$b")) -IncludeEqual -ExcludeDifferent).Count -eq 3) {
                $R.InputObject | Out-String
            }
        }
    }
}

$Result | Select-Object -Unique

Comments

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.