Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:42:53+00:00 2026-06-16T00:42:53+00:00

I’m trying to find the simplest way to query Active Directory, in one of

  • 0

I’m trying to find the simplest way to query Active Directory, in one of two ways:

  1. Given an AD username, find all the groups (INCLUDING nested groups) that the user is a member of.

  2. Given an AD group name, find all the users (including those users in nested groups) that are part of the group.

My app is in VB.NET on the v4.0 framework. I’ve reviewed suggestions from many different Google search results, some of which utilize LDAP and System.DirectoryServices.DirectorySearcher (which I’m thinking might be the best route).

But I’m spinning my wheels and am looking for code samples.

Thank you.

UPDATE:

I’ve got these pieces in place:

<add assembly="System.DirectoryServices, Version=3.5.0.0, etc."/>

<add namespace="System.DirectoryServices.AccountManagement" /> or Imports System.DirectoryServices.AccountManagement

and on this line of code:

Dim ctx As New PrincipalContext(ContextType.Domain)

I still get this error: Type ‘PrincipalContext’ is not defined

When you mention the “using statement,” I assume you meant that I need to reference this namespace. Or did you mean I should do something like this?

Using ctx As New PrincipalContext(ContextType.Domain)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:42:54+00:00Added an answer on June 16, 2026 at 12:42 am

    A Complete example

    This code will list all users in a group AND sub groups by enumerating a given group name.
    also, if a users account is enabled / disabled.

    To use, just call ListADGroupMembers(“Some_Group_Name”).
    This will populate the users full name and mobile number into an array, which you can then loop through.

    Its pretty simple to follow, just read it through.

    Public ADUSers(,) As String
     Public n As Integer = 0
    
     Public Sub ListADGroupMembers(ByVal GN As String)
    
        Dim DirectoryRoot As New DirectoryEntry("LDAP://RootDSE")
        Dim DNC = DirectoryRoot.Properties("DefaultNamingContext")(0).ToString()
        Dim GroupName As String = GN '"G_All_IT_Users"
        Dim GroupMembers As System.Collections.Specialized.StringCollection = GetGroupMembers(DNC, GroupName)
        'Dim GroupMembersMobile As System.Collections.Specialized.StringCollection = GetGroupMembers(DNC, GroupName)
        '  For Each Member As String In GroupMembers
        '    ListBox1.Items.Add(Member)
        'Next Member
    
    End Sub
    
    Public Function GetGroupMembers(ByVal strDomain As String, ByVal strGroup As String) As System.Collections.Specialized.StringCollection
    
        Dim GroupMembers As New System.Collections.Specialized.StringCollection()
    
        Try
            Dim DirectoryRoot As New DirectoryEntry("LDAP://" & strDomain)
            Dim DirectorySearch As New DirectorySearcher(DirectoryRoot, "(CN=" & strGroup & ")")
            Dim DirectorySearchCollection As SearchResultCollection = DirectorySearch.FindAll()
            For Each DirectorySearchResult As SearchResult In DirectorySearchCollection
                Dim ResultPropertyCollection As ResultPropertyCollection = DirectorySearchResult.Properties
                Dim GroupMemberDN As String
                For Each GroupMemberDN In ResultPropertyCollection("member")
                    Dim DirectoryMember As New DirectoryEntry("LDAP://" & GroupMemberDN)
                    Dim DirectoryMemberProperties As System.DirectoryServices.PropertyCollection = DirectoryMember.Properties
                    Dim DirectoryItem As Object = DirectoryMemberProperties("sAMAccountName").Value
                    Dim DirectoryPhone As Object = DirectoryMemberProperties("mobile").Value
                    Dim uac As Object = DirectoryMemberProperties("userAccountControl").Value
    
                    If DirectoryMember.SchemaClassName = "group" Then
                        ' this is a group.                        
                        ListADGroupMembers(DirectoryItem)
                    End If
    
                    If DirectoryMember.SchemaClassName = "user" Then
                        ' this is a user.
                        If Nothing IsNot DirectoryItem Then
                            If AccEnabled(uac) = 1 Then ' check the ad account is enabled
                                GroupMembers.Add(DirectoryItem.ToString())
                                ListBox1.Items.Add(DirectoryItem.ToString() & " " & DirectoryPhone)
    
                                ADUSers(0, n) = DirectoryItem.ToString()
                                ADUSers(1, n) = DirectoryPhone
                                n += 1
                                ReDim Preserve ADUSers(1, n)
                            End If
                        End If
                    End If
    
                Next GroupMemberDN
    
            Next DirectorySearchResult
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    
        Return GroupMembers
    
    
    
    End Function
    
    
    
    
      ' check account is active or not.
    Function AccEnabled(ByVal uac As String) As String
    
        Dim aret As Integer = 0
        Select Case uac
            Case 512 'Enabled 
                aret = 1
            Case 514 ': ACCOUNTDISABLE()
                aret = 0
            Case 528 ': Enabled(-LOCKOUT)
                aret = 1
            Case 530 ': ACCOUNTDISABLE(-LOCKOUT)
                aret = 0
            Case 544 ': Enabled(-PASSWD_NOTREQD)
                aret = 1
            Case 546 ': ACCOUNTDISABLE(-PASSWD_NOTREQD)
                aret = 0
            Case 560 ': Enabled(-PASSWD_NOTREQD - LOCKOUT)
                aret = 1
            Case 640 ': Enabled(-ENCRYPTED_TEXT_PWD_ALLOWED)
                aret = 1
            Case 2048 ' : INTERDOMAIN_TRUST_ACCOUNT()
                aret = 1
            Case 2080 ': INTERDOMAIN_TRUST_ACCOUNT(-PASSWD_NOTREQD)
                aret = 1
            Case 4096 ': WORKSTATION_TRUST_ACCOUNT()
                aret = 1
            Case 8192 ': SERVER_TRUST_ACCOUNT()
                aret = 1
            Case 66048 ': Enabled(-DONT_EXPIRE_PASSWORD)
                aret = 1
            Case 66050 ': ACCOUNTDISABLE(-DONT_EXPIRE_PASSWORD)
                aret = 0
            Case 66064 ': Enabled(-DONT_EXPIRE_PASSWORD - LOCKOUT)
                aret = 1
            Case 66066 ': ACCOUNTDISABLE(-DONT_EXPIRE_PASSWORD - LOCKOUT)
                aret = 0
            Case 66080 ': Enabled(-DONT_EXPIRE_PASSWORD - PASSWD_NOTREQD)
                aret = 1
            Case 66082 ': ACCOUNTDISABLE(-DONT_EXPIRE_PASSWORD - PASSWD_NOTREQD)
                aret = 0
            Case 66176 ': Enabled(-DONT_EXPIRE_PASSWORD - ENCRYPTED_TEXT_PWD_ALLOWED)
                aret = 1
            Case 131584 ': Enabled(-MNS_LOGON_ACCOUNT)
                aret = 1
            Case 131586 ': ACCOUNTDISABLE(-MNS_LOGON_ACCOUNT)
                aret = 0
            Case 131600 ': Enabled(-MNS_LOGON_ACCOUNT - LOCKOUT)
                aret = 1
            Case 197120 ': Enabled(-MNS_LOGON_ACCOUNT - DONT_EXPIRE_PASSWORD)
                aret = 1
            Case 532480 'SERVER_TRUST_ACCOUNT - TRUSTED_FOR_DELEGATION (Domain Controller) 
                aret = 1
            Case 1049088 ': Enabled(-NOT_DELEGATED)
                aret = 1
            Case 1049090 ': ACCOUNTDISABLE(-NOT_DELEGATED)
                aret = 0
            Case 2097664 ': Enabled(-USE_DES_KEY_ONLY)
                aret = 1
            Case 2687488 ': Enabled(-DONT_EXPIRE_PASSWORD - TRUSTED_FOR_DELEGATION - USE_DES_KEY_ONLY)
                aret = 1
            Case 4194816 ': Enabled(-DONT_REQ_PREAUTH)
                aret = 1
            Case Else
                aret = 0
        End Select
    
        AccEnabled = aret
    
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.