# Compare-Object returns only one match of many

**URL:** <https://forums.powershell.org/t/compare-object-returns-only-one-match-of-many/13473>\
**Category:** PowerShell Help\
**Created:** [November 26, 2019, 7:44am UTC](https://forums.powershell.org/t/compare-object-returns-only-one-match-of-many/13473 "2019-11-26T07:44:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddiejc](https://avatars.discourse-cdn.com/v4/letter/e/4da419/32.png) [@eddiejc](https://forums.powershell.org/u/eddiejc)\
**Post date:** [November 26, 2019, 7:44am UTC](https://forums.powershell.org/t/compare-object-returns-only-one-match-of-many/13473/1 "2019-11-26T07:44:34Z")

</div>

I have 2 spreadsheets that I have imported to PowerShell arrays.

They BOTH have USERNAME as a property, but otherwise they have different properties.

In one of them (array1) the USERNAME is unique among 300 entries; in the other (array2), a given USERNAME may have multiple entries among 30,000 entries, & not all of the USERNAMEs will appear in both.

Cycling thru the smaller array1 picking out the matches in array2 takes hours to complete:

```
foreach($item in $array1){ $matches +=@($array2 |where{$\_.USERNAME -eq $($item.USERNAME)})}
```

However, using Compare-Object is much quicker (a matter of a few seconds) BUT only gives the 1st match from array2:

```
$matches =Compare-Object-ReferenceObject $array2 -DifferenceObject $array1 -Property USERNAME -ExcludeDifferent-IncludeEqual-PassThru
```

…so I lose the multiple matches from array2; if I remove the -ExcludeDifferent switch then the SideIndicator results for multiple matches show the 1st of the multiple matches “==” but the subsequent multiple matches as “\<=”.

Is there a way to get Compare-Object to correctly return all the array2 matches? Or is there a better way?

Thanks in advance for your replies! 🙂

Eddie

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 26, 2019, 8:49pm UTC](https://forums.powershell.org/t/compare-object-returns-only-one-match-of-many/13473/2 "2019-11-26T20:49:16Z")

</div>

If I got you right this should speed up the task a little bit:

```
$result = foreach ($item in $array2) {
    $item | Where-Object {
        $item.Username -in $array1.Username
    }
}
$result | Sort-Object -Property username
```

This should need only one run through the $array2.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/compare-object-returns-only-one-match-of-many/13473/3 "2024-05-16T20:31:01Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


