Option Explicit Const ForAppending = 8 Const ForReading = 1 Const ForWriting = 2 Dim objOutput, objInput, fso, adoConnection, objRoot, objShell, strDomain, strBase, strLine, arrLine, objDomain, strFilter, strAttribs, strDepth, strQuery, adoRecordSet Dim bolFound, strGiven Set fso = CreateObject("Scripting.FileSystemObject") Set objInput = fso.OpenTextFile("C:\scripts\input.csv", ForReading) Set objOutput = fso.OpenTextFile("C:\scripts\output.txt", ForWriting, True) Set adoConnection = CreateObject("ADODB.Connection") Set objRoot = GetObject("LDAP://rootDSE") Set objShell = CreateObject("WScript.Shell") strDomain = objRoot.Get("defaultNamingContext") Set objDomain = GetObject("LDAP://" & strDomain) strBase = "<" & objDomain.ADsPath & ">" adoConnection.Open "Data Source=Active Directory Provider;Provider=ADsDSOObject" Do Until objInput.AtEndOfStream strLine = objInput.ReadLine arrLine = Split(strLine,",") 'wscript.echo arrLine(0) & " " & arrLine(1) strFilter = "(&(objectCategory=person)(objectClass=user)(sn=" & arrLine(0) & "))" strAttribs = "sAMAccountName,givenName" strDepth = "subTree" ' ' 'Search the directory for the account name by last name strQuery = strBase & ";" & strFilter & ";" & strAttribs & ";" & strDepth Set adoRecordSet = adoConnection.Execute(strQuery) If adoRecordSet.recordCount = 0 Then wscript.echo arrLine(1) & " " & arrLine(0) & vbTab & "No users found." Else bolFound = 0 While Not adoRecordSet.EOF strGiven = adoRecordSet.Fields("givenName").Value If strComp(strGiven,arrLine(1)) = 0 Then bolFound = 1 objOutput.writeline adoRecordSet.Fields("sAMAccountName").Value End If adoRecordSet.MoveNext Wend if bolFound = 0 Then wscript.echo arrLine(1) & " " & arrLine(0) & " Could not match." & vbTab &_ "found: " & strGiven & vbTab & "input:" & arrLine(1) End If End If Loop Set objOutput = Nothing Set objInput = Nothing Set fso = Nothing