Results 1 to 21 of 21

Thread: Get all members of an Active Directory group or local group

Threaded View

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Get all members of an Active Directory group or local group

    You can use this to find a list of all of the members of any group in AD. You will need to add a reference to System.DirectoryServices first though.
    vb.net Code:
    1. 'Change the OU path and group name to suit your environment
    2. Dim GroupDE As New DirectoryEntry("LDAP://CN=YourGroupName,OU=YourGroupsOU,DC=yourdomainname,DC=com")
    3.  
    4. Dim Members As Object = GroupDE.Invoke("Members", Nothing) '<<< Get members
    5. For Each Member As Object In CType(Members, IEnumerable)  '<<< loop through members
    6.   Dim CurrentMember As New DirectoryEntry(Member) '<<< Get directoryentry for user
    7.   MessageBox.Show(CurrentMember.Name.Remove(0, 3))  '<<< Show each user's name in a messagebox
    8. Next
    9.  
    10. 'NOTE: You should also dispose of each DirectoryEntry that you use, either by using the Dispose method or by using a "Using" statement. I haven't included this in my example above just to keep it short and to the point.

    As you can see in my example, I am just displaying all of the members names in a messagebox but if you want to get different attributes instead of just the name then remove that line and use the DirectoryEntry object for each member to get whatever attributes you want

    Note that you can also get group members by using the "members" attribute of a group (e.g cast GroupDE.Properties("members").Value to an array of strings and each string will be the full LDAP path to each member so you can then bind a new DirectoryEntry to that path) but I believe there are some slight differences in the way that this works when compared to just invoking the Members method (like I do in the code example above).

    Hope it helps someone out

    Chris
    Last edited by chris128; Dec 30th, 2013 at 02:20 PM. Reason: Improved code example and explanation
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width