Finding all groups that contain the same members in Active Directory

Finding all groups that contain the same members in Active Directory

Active Directory users are put into groups for categorization and access privilege purposes. However, as an organization grows, there may be instances of repetition of groups, or an administrator may be required to monitor the groups that a specific user is present in. Manual search in this case is time-consuming, so administrators turn to PowerShell.
 
Using PowerShell,  it is possible to identify the groups that have the same members using the adsisearcher commandlet. Here's the script:
 
  1. $Searcher = [adsisearcher]'(member=*)'
  2. $Searcher.PageSize = 500
  3. $Searcher.FindAll() |  ForEach-Object {
  4.  
  5.     New-Object -TypeName PSCustomObject -Property @{
  6.         DistinguishedName = $_.Properties.distinguishedname[0]
  7.         Member = $_.Properties.member -join ';'
  8.     }
  9.  
  10. } | Group-Object -Property member |
  11. Where-Object {$_.Count -gt 1} |
  12. Sort-Object -Property Count -Descending