Get members of Active Directory group

November 10, 2009 — 2 Comments

The code below will search an Active Directory container/OU for a specified group and will then loop through the members of that group and add each user’s name to a listbox:

Dim GroupSearcher As New DirectorySearcher
‘!!!Change the OU path, domain and domain admin details!!!
Dim GroupSearchRoot As New DirectoryEntry(“
LDAP://OU=YourGroupsOU,DC=yourdomainname,DC=com“, “Your_Domain_Admin”, “Admin_Password”)

With GroupSearcher  
       .SearchRoot = GroupSearchRoot    
       .Filter = “(&(ObjectClass=Group)(CN=YourGroupName))”  ‘<<< Change the Group name to search for here!!
End With

Dim Members As Object = GroupSearcher.FindOne.GetDirectoryEntry.Invoke(“Members”, Nothing) ‘<<< Get members
For Each Member As Object In CType(Members, IEnumerable)  ‘<<< loop through members
    Dim CurrentMember As New DirectoryEntry(Member) ‘<<< Get user
    ListBox1.Items.Add(CurrentMember.Name.Remove(0, 3))  ‘<<< Add user’s name to listbox

I have also posted this on vbforums if you find it easier to read/copy from there:

2 responses to Get members of Active Directory group


    This is amazing, simple, and clean. How would you display the NT ID usernames instead of the full name?


    Just grab the sAMAccountName property instead of the Name. It would be slightly different as the Name property is actually part of the DirectoryEntry class and sAMAccountName is just an attribute on the AD object. So you would just replace the line that uses “CurrentMember.Name” with “CurrentMember.Properties(“sAMAccountName”).Value”

    Hope that helps

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s