Compare members of 2 Active Directory groups

Compare members of 2 Active Directory groups

Compare members of 2 Active Directory groups

Compare members of 2 Active Directory groups. If you have to compare between 2 Active Directory groups, the members that belong to each of them. You can write down a few Powershell lines that helps with that.

Example:

$groupA= (Get-ADGroupMember -Identity «groupA» -Server domaincontroller.company.com -Recursive).SamAccountName
#You store on this variable, all active directory SamAccountNames from the members that belongs to the groupA and its nested groups.

$groupB = (Get-ADGroupMember -Identity «groupB» -Server domaincontroller.company.com -Recursive).SamAccountName
#You store on this variable, all active directory SamAccountNames from the members that belongs to the groupB and its nested groups.

$difference= @()
#You create an empty array, that will be used later to store the members difference between the 2 Active Directory groups.

foreach ($member in $groupB){
if($groupA -notcontains $member){
$difference += $member
}
}
#We will go, one by one, for each member of GroupB, checking if that member of GroupB also exists in Group A, and if not we will add that member into the define empty array, named $difference.

write-host $difference.count

#We will write down the number of difference members between 2 Active Directory Groups.

$groupA | Out-File -FilePath D:\temp\groupA.txt

#We create a temporal txt file with all the members of GroupA.

$groupB | Out-File -FilePath D:\temp\groupB.txt

#We create a temporal txt file with all the members of GroupB.

$difference | Out-File -FilePath D:\difference.txt

#We create a temporal txt file with all the members that belong to groupB, but not belong to groupA.

You can get more details about, Get-AdGroupMember command: here

More content regarding Skype for Business, can be found on the blog here.

Deja un comentario

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.