This script will search AD from a search root with attribute filter and export the output to a csv file.
On Error Resume Next Const ADS_SCOPE_SUBTREE = 2 Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objFile = objFileSystem.OpenTextFile("results_" & date & ".csv", 2, True, 0) Set objConnection = CreateObject("ADODB.Connection") objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" Set objCommand = CreateObject("ADODB.Command") objCommand.ActiveConnection = objConnection Set objRootDSE = GetObject("LDAP://RootDSE") objCommand.CommandText = "SELECT aDSPath, sAMAccountName, givenName, sn, mail, telephoneNumber " &_ "FROM 'LDAP://OU=Users,OU=Company," & objRootDSE.Get("defaultNamingContext") & "'" Set objRootDSE = Nothing objCommand.Properties("Page Size") = 1000 objCommand.Properties("Timeout") = 600 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCommand.Properties("Cache Results") = False Set objRecordSet = objCommand.Execute While Not objRecordSet.EOF Set objEntry = GetObject(objRecordSet.Fields("aDSPath")) strClass = "" : strClass = objEntry.Class If strClass = "user" Then strSAMAccountName = "" : strGivenName = "" : strSN = "" : strMail = "" : strTelephoneNumber = "" 'strUPN = "" , strInitials = "" strSAMAccountName = objRecordSet.Fields("sAMAccountName") strGivenName = objRecordSet.Fields("givenName") strSN = objRecordSet.Fields("sn") strMail = objRecordSet.Fields("mail") strTelephoneNumber = objRecordSet.Fields("telephoneNumber") 'strUPN = objRecordSet.Fields("userPrincipalName") 'strInitials = objRecordSet.Fields("initials") objFile.WriteLine strSAMAccountName & ";" & strGivenName & ";" & strSN & ";" & strMail & ";" &_ strTelephoneNumber ' & strUPN & "," & strInitials End If objRecordSet.MoveNext Wend objConnection.Close Set objRecordSet = Nothing Set objCommand = Nothing Set objConnection = Nothing Set objFile = Nothing Set objFileSystem = Nothing