Getting all users from Active Directory using LDAP query

Occasionally I have found it necessary to query Active Directory to get a list of all users. This is what I did. If it helped let me know.
 
Dim DirSearcher As New DirectorySearcher()
        DirSearcher.SearchRoot = New DirectoryEntry("LDAP://" & System.Environment.UserDomainName)
        DirSearcher.Filter = "(&(objectclass=user)(objectcategory=person))"
        DirSearcher.PageSize = 1000
        DirSearcher.SearchScope = SearchScope.Subtree
        ‘DirSearcher.PropertiesToLoad.Add("sAMAccountName")

        Dim DirSearchResultCol As SearchResultCollection
        DirSearchResultCol = DirSearcher.FindAll()

        Dim objResult As SearchResult
        For Each objResult In DirSearchResultCol
            Debug.Print(objResult.Properties("sAMAccountName")(0))
        Next

 
 

Advertisements
  1. Leave a comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: