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
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.