PowerShell/Active Directory - Compare two users accounts membership

2011/01/28 | less than 1 minute read |

Here is a PowerShell Tip to compare 2 Active Directory user account membership

Using ActiveDirectory module

# Import ActiveDirectory Module
Import-Module ActiveDirectory

function Compare-ADUserGroups
{
 param (
  [string] $FirstUser = $(Throw "SAMAccountName required."),
  [string] $SecondUser = $(Throw "SAMAccountName required.")
 )

 $a = (Get-ADUser $FirstUser).MemberOf
 $b = (Get-ADUser $SecondUser).MemberOf
 Compare-Object -referenceObject $a -differenceObject $b
}

Compare-ADUserGroups -firstuser useraccount1 -SecondUser useraccount2

Using Quest Active Directory

# Load Quest snapin
Add-PSSnapin Quest.ActiveRoles.ADManagement

function Compare-ADUserGroups
{
 param (
  [string] $FirstUser = $(Throw "SAMAccountName required."),
  [string] $SecondUser = $(Throw "SAMAccountName required.")
 )

 $a = (Get-QADUser $FirstUser).MemberOf
 $b = (Get-QADUser $SecondUser).MemberOf
 Compare-Object -referenceObject $a -differenceObject $b
}

Compare-ADUserGroups -firstuser useraccount1 -SecondUser useraccount2

Leave a comment