A fast and universal way to get all the user information on the active directory with export to csv.

Don't forget to make a reference to system.directoryServices.

'CONNECT 
'with Authentication 
'Public enTry As System.DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry("LDAP://YOURSERVER", "USER", "PASSWORD", DirectoryServices.AuthenticationTypes.Secure) 
'no Authentication 
Public enTry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://YOURSERVER") 

Sub GETUSERS() 
Dim Bestand As String = Application.StartupPath + "/AS_User.csv" 
Dim mySearcher As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(enTry) 
Dim resEnt As System.DirectoryServices.SearchResult 

mySearcher.Filter = "(&(objectCategory=person)(objectClass=user))" 

Try 
FileClose(1) 
FileOpen(1, Bestand, OpenMode.Output, OpenAccess.Write, OpenShare.Shared) 
Print(1, "ACCOUNT;FORENAME;GIVENAME;EMAIL;LOCATION;TEL;FAX;MOBILE" + vbCrLf) 
Catch 
MsgBox("CLOSE THIS FILE : " + vbCrLf + Bestand, MsgBoxStyle.Critical, "Output file locked.") 
Exit Sub 
End Try 

PB.Maximum = mySearcher.FindAll.Count 
PB.Value = 0 
For Each resEnt In mySearcher.FindAll() 
PB.Value = PB.Value + 1 
Dim SA As String = " " 
Dim SN As String = " " 
Dim NAME As String = " " 
Dim MAIL As String = " " 
Dim TEL As String = " " 
Dim LOC As String = " " 
Dim FAX As String = " " 
Dim Mobile As String = " " 

NAME = ReadEntry("givenname", resEnt) 
SA = ReadEntry("samaccountname", resEnt) 
SN = ReadEntry("sn", resEnt) 
MAIL = ReadEntry("mail", resEnt) 
TEL = ReadEntry("telephoneNumber", resEnt) 
LOC = ReadEntry("physicalDeliveryOfficeName", resEnt) 
FAX = ReadEntry("facsimileTelephoneNumber", resEnt) 
Mobile = ReadEntry("mobile", resEnt) 

'Show only valid NAME's 
If NAME <> "" And SN <> "" Then 
Print(1, SA + ";" + NAME + ";" + SN + ";" + MAIL + ";" + LOC + ";" + TEL + ";" + FAX + ";" + Mobile + vbCrLf) 
End If 
Next 
FileClose(1) 
Try 
Process.Start(Bestand) 'Starts with excel/calc if assosiated 
Catch ex As Exception 
End Try 
End Sub 
Public Function ReadEntry(ByVal ENTRYNAME As String, ByVal resEnt As System.DirectoryServices.SearchResult) As String 
Try 
Dim RETVALUE As String = Trim(resEnt.Properties(ENTRYNAME)(0).ToString) 
Return FixNull(RETVALUE) 
Catch 
Return "" 
End Try 

End Function 
Public Function FixNull(ByVal dbvalue) As String 
If dbvalue Is DBNull.Value Then 
Return " " 
Else 
Return Trim(dbvalue.ToString) 
End If 
End Function

Dani AI

Generated

Nice share, . A couple of tweaks will make this scale and run faster in big directories: enable paging so you are not capped at 1,000 results, load only the attributes you plan to export, and (optionally) filter out disabled accounts. Also consider a serverless bind to the domain DN or the GC when you need to search across the forest. References: DirectorySearcher PageSize (paged searches), the default AD LDAP page size of 1,000, bitwise filter syntax for userAccountControl, PropertiesToLoad behavior, and binding to GC. (learn.microsoft.com)

Here is a trimmed VB.NET example that folds those changes in and is CSV-friendly (quote values when needed). It avoids re-running the query, pages through all results, and safely checks each attribute.

Imports System.DirectoryServices

Using root As New DirectoryEntry("LDAP://DC=yourdomain,DC=com")
    Using ds As New DirectorySearcher(root)
        ds.Filter = "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"
        ds.SearchScope = SearchScope.Subtree
        ds.PageSize = 1000
        ds.CacheResults = False
        ds.PropertiesToLoad.AddRange(New String() {
            "samAccountName","givenName","sn","mail",
            "physicalDeliveryOfficeName","telephoneNumber",
            "facsimileTelephoneNumber","mobile"})

        Using results As SearchResultCollection = ds.FindAll()
            For Each r As SearchResult In results
                Dim row = New String() {
                    GetProp(r,"samAccountName"), GetProp(r,"givenName"),
                    GetProp(r,"sn"), GetProp(r,"mail"),
                    GetProp(r,"physicalDeliveryOfficeName"),
                    GetProp(r,"telephoneNumber"),
                    GetProp(r,"facsimileTelephoneNumber"),
                    GetProp(r,"mobile")}
                ' Write row to CSV here...
            Next
        End Using
    End Using
End Using

Private Shared Function GetProp(r As SearchResult, attr As String) As String
    If r.Properties.Contains(attr) AndAlso r.Properties(attr).Count > 0 Then
        Return CStr(r.Properties(attr)(0))
    End If
    Return String.Empty
End Function

Two more tips: dispose the SearchResultCollection (Using block above) to prevent unmanaged memory leaks; and if you prefer higher-level APIs, the AccountManagement classes (e.g., UserPrincipal + PrincipalSearcher) can simplify common user queries. (learn.microsoft.com)

And yes, , CSVDE is great for quick one-offs; the code path shines when you want custom filters, fields, or post-processing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.