PowerShell to Find Users in Multiple AD Groups

PowerShell to Find Users in Multiple AD Groups

Another of the engineer that I would with came to me with a scripting issue.  They had a list of 185 users account that he needed to find if they were members of a list of 6 AD groups.  So that is where PowerShell and I come into this issue.  So after thinking about this issue, it all came down to a comparing the lists.

I’m Not Lazy, I Just Don’t Like the Same Stuff Over Again

I live by the motto, “if you have to do it more than once, script it“.  I have lived by this mantra for a long time, and has basically helped me to get into the position that I in today.   All that aside, I racked my brain on how to complete this task as i normally start out over complicating the issue.  So after having a complex script that still didn’t get the results, I remember the command Compare-Object.

$users = Get-Content C:\scripts\logs\users.txt
$CitrixGroups = Get-Content C:\scripts\logs\Citrix_Groups.txt
foreach ($group in $CitrixGroups){
     $AllGroupMembers = (get-adgroup -server $domain -Identity $group | Get-ADGroupMember -Recursive).SamAccountName
     (Compare-Object -ReferenceObject $AllGroupMembers -DifferenceObject $users -IncludeEqual -ExcludeDifferent).inputobject | Out-file C:\Scripts\logs\Citrix\$group.txt 

This script loads the $User variable with the user accounts from a text document and them gets all of the group members from the groups in the variable as $AllGroupMembers one at a time and does a compare.  The switches -IncludeEqual and -ExcludeDifferent sorts the results to see exactly what I want to see.  This also generates a text file using the group name as the filename and includes the members that are included in the $User variable.


A Blog of Another Color

I would like to introduce you to a colleague of mine, and the person that I wrote this script for, Kris Davis.  His blog, XenApplePie, which if you can’t tell by the name, is about all things Citrix.  He is just getting the blog baked and ready for all to enjoy.  He has a ton of knowledge to share about Citrix as he has been supporting it for years.

1 thought on “PowerShell to Find Users in Multiple AD Groups”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.