How Can I Get the Full Name of a Windows NT 4.0 User When All I Have is Their Logon Name?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the full name of a user in a Windows NT 4.0 domain when all I have is the user’s logon name?

— NH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, NH. In case you’re wondering, after a less-than-auspicious beginning the Scripting Guy who writes this column started to get used to swimming laps every night; in fact, he even started to enjoy the experience a little. Of course, no sooner did he do so than the Scripting Son came up with a new idea: core training. “That’s what everyone does,” remarked the Scripting Son. “Don’t you want to have a strong core?”

Well, to be perfectly honest: no. After all, the Scripting Dad has survived this long without having a strong core; surely he could get by for at least a little while longer with a weak core.

He became even more convinced of this after he found out what it actually meant to do core training. As near as he can tell, core training primarily involves contorting your body into an unnatural position, having someone stick a 25-pound weight on you, and then holding that position until you pass out or until time is up. For example, in one exercise you get down into a sort of pushup-like position, balancing on your toes and forearms. The Scripting Son then plops a 25-pound weight on your back and tells you to hold yourself up for 1 minute.

Admittedly, that doesn’t sound too bad, although it is. But it’s still something even the Scripting Guy who writes this column can handle. It only starts to get bad when the Scripting Son says, “You know, I can do 2 minutes.” And then the Scripting Dad says, “What, just 2? I can do 3 – no, wait: I can do 4!”

We don’t know if any of you have ever had the desire to see a Scripting Guy cry, but, if you do, just swing by the gym some night when he’s trying to do 4 minutes.

Or, if you prefer, swing by his office on the day he gets his performance review. Either way he’s bound to be crying.

Of course, we’d hate to see you cry, NH. So here’s a script that reports back the full name of a user (Ken Myer) when all you have is the user’s logon name (kenmyer):

strDomain = “fabrikam”
strUserName = “kenmyer”

Set objUser = GetObject(“WinNT://” & strDomain & “/” & strUserName) Wscript.Echo objUser.FullName

Yes, it is a very simple little script. (And not by coincidence: thanks to the Scripting Games we were looking for something we could answer quickly and easily.) To start off with we simply assign values to a pair of variables: strDomain gets the name of our domain and strUserName gets the logon name of our user. We then use this line of code to bind directly to the user account in the NT 4.0 domain:

Set objUser = GetObject(“WinNT://” & strDomain & “/” & strUserName)

Once we’ve made the connection we need just one more line of code to report back the user’s FullName:

Wscript.Echo objUser.FullName

And that’s it; that’s all we have to do.

Of course, we should note that, as an added bonus, this very same script can be used to retrieve the full name of a local user; in that case, you simply bind to the SAM (security account manager) on a computer as opposed to a domain. Here’s pretty much the same script we just showed you, slightly revised to work with local user accounts:

strComputer = “fabrikam”
strUserName = “kenmyer”

Set objUser = GetObject(“WinNT://” & strComputer & “/” & strUserName) Wscript.Echo objUser.FullName

If that looks vaguely familiar, well, it should: all we did was change the name of the variable strDomain to strComputer. Other than that ….

While we’re on the subject, the flip side to this question is this: if all I have is the user’s full name how can I get his or her logon name?

Note. In this era of CDs and DVDs does anyone even know what we’re talking about when we say “flip side?”

That’s a good point: nobody ever knows what we’re talking about when it comes to this column, do they?

OK, so how do you get the logon name if all you have is the full name? Well, with an Active Directory domain the answer would be obvious: you’d do an ADO search on the domain. Unfortunately, you can’t search for user accounts in an NT 4.0 domain or a local SAM. Fortunately, though, that doesn’t mean you can’t locate a particular user, it just means you have to go about it like this:

strDomain = “fabrikam”

Set colAccounts = GetObject(“WinNT://” & strDomain & “”) colAccounts.Filter = Array(“user”)

For Each objUser In colAccounts If objUser.FullName = “Ken Myer” Then Wscript.Echo objUser.Name Exit For End If Next

What we’re doing with this script is, first of all, binding to the domain itself; notice that we don’t specify a user account in our binding string:

Set colAccounts = GetObject(“WinNT://” & strDomain & “”)

After making the connection we then set the Filter property to user; this ensures that our collection will contain only user accounts, weeding out groups, printers, services, and other entities that might otherwise be there. That’s what we do here (and, yes, values to the Filter property must be passed as an array, even if we’re filtering on only one object type):

colAccounts.Filter = Array(“user”)

We then set up a For Each loop to walk through the entire collection. Inside that loop we check each individual user account to see if the FullName property is equal Ken Myer. If it is, then we echo back the value of the Name property and then exit the loop. If it isn’t, then we loop around and try again with the next account. That’s what this block of code does:

If objUser.FullName = “Ken Myer” Then
    Wscript.Echo objUser.Name 
    Exit For
End If

Admittedly, it’s not as cool as doing an ADO search, but it’s the best we can do. And it will work, both with NT 4.0 domains and with local users.

We see you have another question. The answer to that one is no: you do not need to have a strong core in order to enter the 2007 Winter Scripting Games, and have a shot at winning a Dr. Scripto Bobblehead doll or a copy of the book Windows PowerShell in Action. (Well, not unless you usually write scripts with 25-pound weights strapped to your back.) Just enter an event (any event) and you’ll automatically be entered in the drawing for one of those prizes. Score 60 points in any one division and you’ll earn a Certificate of Excellence signed by the Scripting Guys. Pretty cool, huh?

Note. Why are we giving away Certificates of Excellence? Well, as it turned out, awhile back we bought a whole bunch of these awards, the idea being that we would hand them out to each other every time we did something really good. Let’s just say that we have plenty of certificates left over ….


Discussion is closed.

Feedback usabilla icon