I have scripted a LDAP (Lightweight Directory Access Protocol) query to demonstrate how to return multiple Active Directory objects in your domain. The script determines your domain name, employs a filter to select users who match a sAMAccountName parameter, defines a list of attributes required, positions to the first record, and outputs a semi-colon (;) delimited file containing the distinguishedName, sAMAccountName, userPrincipalName, givenName, and sn attributes.

NOTE: See tip 10015 ยป How can I use VBScript to return all the users in an OU (Organizational Unit)?

GetUsers.vbs contains:

                              On Error Resume Next                              Dim objConnection, objCommand, objRootDSE, strDNSDomain                              Dim strFilter, strQuery, objRecordSet, objArgs                              Set objArgs = Wscript.Arguments                              if objArgs.Count = 0 then                                Wscript.Echo  "sAMAccountName argument required. ""*"" or ""J*"" or etc.."                               WScript.Quit (1)                              End If                              sam = objArgs(0)                              Set objConnection = CreateObject("ADODB.Connection")                              Set objCommand = CreateObject("ADODB.Command")                              objConnection.Provider = "ADsDSOOBject"                              objConnection.Open "Active Directory Provider"                              Set objCommand.ActiveConnection = objConnection                              Set objRootDSE = GetObject("LDAP://RootDSE")                              'Get domain                              strDNSDomain = objRootDSE.Get("defaultNamingContext")                              strBase = "<LDAP://" & strDNSDomain & ">"                              'Define the filter elements                              strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & sam & "))"                              'List all attributes you will require                              strAttributes = "distinguishedName,sAMAccountName,givenName,sn,userPrincipalName"                              'compose query                              strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"                              objCommand.CommandText = strQuery                              objCommand.Properties("Page Size") = 99999                              objCommand.Properties("Timeout") = 300                              objCommand.Properties("Cache Results") = False                              Set objRecordSet = objCommand.Execute                              objRecordSet.MoveFirst                              Do Until objRecordSet.EOF                                  strDN = objRecordSet.Fields("distinguishedName")                                  strGN = objRecordSet.Fields("givenName")                                  strsn = objRecordSet.Fields("sn")                                  strSA = objRecordSet.Fields("sAMAccountName")                                  strUN = objRecordSet.Fields("userPrincipalName")                                  Wscript.Echo 
" & strDN &
;
& strSA &
;
& strUN &
;
& strGN &
;
& strsn &
" objRecordSet.MoveNext Loop ' Clean up. objConnection.Close Set objConnection = Nothing Set objCommand = Nothing Set objRootDSE = Nothing Set objRecordSet = Nothing