Getting all groups that an Active Directory user is a member of in

Hello all. This is an extremely late post today. I was extremely busy with work issues last night and this morning. I am extremely tired and so ready to go to bed and not think about coding today.

Anyway for my topic today we want to find out all groups a given user belongs to. This was written some time ago at a time when we were trying to reorganize an active directory structure.

Make it a great day!

Dim userName As String = "your user name"
        Dim root As DirectoryEntry = New DirectoryEntry("LDAP://your domain name")
        Dim _objDirSearcher As DirectorySearcher = New DirectorySearcher(root)
        _objDirSearcher.Filter = "(&(objectCategory=user)(name=" + userName.ToString() + "))"
        Dim arr As Object = Nothing
        Dim arrcol As Object() = Nothing
            ‘get all the user objects matching with the search pattern given
            Dim _objResults As SearchResultCollection = _objDirSearcher.FindAll()
            ‘loop with in each object
            Dim _objResult As SearchResult
            For Each _objResult In _objResults
                ‘Check for properties available
                If (Not _objResult Is Nothing) And _objResult.GetDirectoryEntry().Properties.Count > 0 Then
                    ‘verify for the mobile property not null
                    If Not _objResult.GetDirectoryEntry().Properties("memberOf").Value Is Nothing Then
                        If TypeOf _objResult.GetDirectoryEntry().Properties("memberOf").Value Is Object() Then
                            arr = CType(_objResult.GetDirectoryEntry().Properties("memberOf").Value, Object())
                        ElseIf TypeOf _objResult.GetDirectoryEntry().Properties("memberOf").Value Is Object Then
                            arr = CType(_objResult.GetDirectoryEntry().Properties("memberOf").Value, Object)
                        End If
                        Exit For
                    End If
                End If
        Catch e As Exception
            Return col
        End Try


  1. #1 by John on July 25, 2008 - 9:52 am

    Outstanding.  I tried this for months and gave up a few years ago.

  2. #2 by Kelly on July 25, 2008 - 10:06 am

    Thanks John. I never know if people actually read this stuff so it is good to see somebody actually did. 🙂

  3. #3 by Brian on April 22, 2016 - 3:59 pm

    It’s pretty easy if you just do this, although if you are looking for a refined query with conditions it’s not the best. It does get the job done though.

    Function Get_Groups()
    Dim NTUSERID = “userX” ‘or pass it to the function if you prefer
    Dim user = GetObject(“WinNT://YourDomain” & “/” & NTUSERID & “,user”)
    For Each item In user.Groups
    MsgBox(item.Name) ‘or some other action here
    End Function

  4. #4 by GFX on September 20, 2016 - 8:57 pm

    I’m not really sure if I’m super late for this, but if somebody is alive on this thread please let me know. I have a few questions.

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: