Thursday, June 11, 2009

VBScript: Find username by AD fullname

I needed to find the username for a large number of active directory users. Turns out it's actually quite easy to do this with VBScript. Save the following script as a VBScript, for instance username.vbs. Change "domainname" to your own domain name.

Set oArgs = WScript.Arguments
Set objDomain = GetObject("WinNT://domainname")
objDomain.Filter = Array("User")
str2Find = oArgs(0)
For Each aUser In objDomain
If LCase(str2Find) = LCase(aUser.FullName) Then
Wscript.Echo str2Find & "=" & aUser.Name
strUser = aUser.Name
End If
Set objDomain = Nothing

Now you can search a username by running: cscript username.vbs "Full Username"

No comments: