# Compare adgroup members of 2 domains

**URL:** <https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609>\
**Category:** PowerShell Help\
**Created:** [May 21, 2021, 5:26pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609 "2021-05-21T17:26:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertrobert](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@robertrobert](https://forums.powershell.org/u/robertrobert)\
**Post date:** [May 21, 2021, 5:26pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/1 "2021-05-21T17:26:43Z")

</div>

hello guys,  
we are in the process of merging two domains so now we want to compare the AD group memberships of both  
first i retrieved all groups with the same name

```auto
$groupsdomain1 = (Get-ADGroup -Filter *).name
$groupsdomain2 = (Get-ADGroup -Filter * -Server "domain2.com").name 
$equalgroups = Compare-Object $groupsdomain1 $groupsdomain2 -ExcludeDifferent -IncludeEqual

```

i exported those groups into csv …now i’m trying to retrieve the members of those groups

```auto
$csv = Import-Csv C:\temp\grpstocompare.csv
foreach($grp in $csv){
Compare-Object (Get-ADGroupMember $grp.InputObject).name (Get-ADGroupMember $grp.InputObject -Server "domain2.com" ).name | select inputobject,sideindicator,@{n='group';e={$grp.inputobject}}
}

```

now this works with some errors.

> user1… \<= grp6  
> user2… =\> grp2  
> user3… \<= grp2  
> user4 \<= grp3  
> Compare-Object:  
> Line |  
> 2 | … bject).name (Get-ADGroupMember $grp.InputObject -Server "[domain2.com](http://domain2.com) …  
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> | Cannot bind argument to parameter ‘DifferenceObject’ because it is null.  
> user5 =\> grp2  
> user5 \<= grp1

my question is …  
1- why am i getting this error?  
2- does get-adgroupmember retrieve users,computers and contacts?  
3- is there a better way to do this ?

thank you

---

<div class="post-metadata">

**Author:** ![robertrobert](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@robertrobert](https://forums.powershell.org/u/robertrobert)\
**Post date:** [May 22, 2021, 10:40am UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/2 "2021-05-22T10:40:33Z")

</div>

why is it that i never get help on this forum ?  
am i doing something wrong ?  
i never even get a reply

---

<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:** [May 22, 2021, 12:09pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/3 "2021-05-22T12:09:05Z")

</div>

> [@robertrobert](#):
>
> am i doing something wrong ?

No. You don’t. 😉

I personally don’t have experiences with a multi domain environment. And I don’t have access to one to test at the moment.

First I’d recommend to use the DN or sAMAccountName for the comparison. They are unique inside one domain. And I’d write my code a little more verbose … like this:

```auto
$groupsdomain1 = Get-ADGroup -Filter *
$groupsdomain2 = Get-ADGroup -Filter * -Server 'domain2.com'
$equalgroups = Compare-Object -ReferenceObject $groupsdomain1 -DifferenceObject $groupsdomain2 -Property 'sAMAccountName' -ExcludeDifferent -IncludeEqual -PassThru

```

Now … why do you export the result and import it again. You can work with the variable `$equalgroups`.

> [@robertrobert](#):
>
> now this works with some errors.
> 
> ```auto
> argument to parameter ‘DifferenceObject’ because it is null.
> 
> ```
> 
> 1- why am i getting this error?

Is it possible that you have groups in your second domain without any members or without “_names_”? And again - I’d use another property than the name.

> [@robertrobert](#):
>
> 2- does get-adgroupmember retrieve users,computers and contacts?

I’d say yes? Did you try? It should be easy to figure out. 😉

> [@robertrobert](#):
>
> 3- is there a better way to do this ?

That depends pretty much on your expectations. Does it do what you need? Even if there are error messages - when you know where they come from and why it still might satisfy your requirements. 😉

---

<div class="post-metadata">

**Author:** ![robertrobert](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@robertrobert](https://forums.powershell.org/u/robertrobert)\
**Post date:** [May 22, 2021, 4:16pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/4 "2021-05-22T16:16:12Z")

</div>

finally someone replied 😄  
1st thank you for replying  
thank you for the recommendation as indeed … some users might not have names so i’ll use the samaccountname property  
and it turned out get-adgroupmember doesn’t get external contacts…  
so i’ll try something else

---

<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:** [May 22, 2021, 5:39pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/5 "2021-05-22T17:39:24Z")

</div>

> [@robertrobert](#):
>
> and it turned out get-adgroupmember doesn’t get external contacts…

That’s correct. Usually those members are irrelevant as long as it is not a distribution group. But if you need all members including contacts you can use the “_Members_” property of the ADGroup.

```auto
Get-ADGroup -Identity 'GroupName' -Properties Members | 
    Select-Object -ExpandProperty 'Members'

```

---

<div class="post-metadata">

**Author:** ![robertrobert](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@robertrobert](https://forums.powershell.org/u/robertrobert)\
**Post date:** [May 22, 2021, 9:40pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/6 "2021-05-22T21:40:09Z")

</div>

well , the groups i want to compare doesn’t have external users it seems  
i found out using this

```auto
$csv = Import-Csv C:\Users\user\Desktop\grpstocompare.csv
foreach ($grp in $csv){
$obj = (Get-ADGroup -Identity $grp.InputObject).distinguishedname
Get-ADObject -Filter {(memberof -eq $obj) -and (objectclass -eq 'contact')}
}

```

so what i’ll do is compare the groups that exists on both domains using the get-adgroupmember  
then i’ll merge the rest of the groups from domain2 to domain 1  
this should solve it  
thanks alot @Olaf

have a nice day

---

<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:** [May 22, 2021, 9:52pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/7 "2021-05-22T21:52:10Z")

</div>

> [@robertrobert](#):
>
> this should solve it

Great that is was helpful and that you found a solution.

And thanks for sharing. 👍🏽 🙂

---

<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:24pm UTC](https://forums.powershell.org/t/compare-adgroup-members-of-2-domains/16609/8 "2024-05-16T20:24:30Z")

</div>


