How Can I Get Just the User Name When Using the Win32_ComputerSystem Class?

ScriptingGuy1

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m using the Win32_ComputerSystem class to get the name of the logged-on user. The name comes back formatted like this: Domain\Name. How can I extract just the user name?

— KW

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KW. As our much-beloved and highly-respected editor reminded us the other day, instead of spending all our time telling people how to extract paragraphs formatted with a specific style from Word documents we promised that every now and then we’d take time to address more basic questions. Having made that promise we then just as quickly forgot about it. (But, in our defense, it’s awfully hard to resist the thrill and excitement that comes with extracting paragraphs formatted with a specific style from Word documents.)

So we’ll make amends today. As many of you know, you can use a simple little script like this to determine the user currently logged-on to a computer:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery(“Select * From Win32_ComputerSystem”)

For Each objItem in colItems Wscript.Echo objItem.UserName Next

Note. One caveat. This works pretty good on Windows XP and Windows Server 2003. On Windows 2000, however, it always returns Null unless an administrator is logged on to the computer. Therefore, this is not the answer to the age-old question, “How can I determine who’s logged on to a particular computer?” (Unless, of course, all your users happen to be local administrators.) Unfortunately, there’s really not a good answer to that question.

As KW noted, though, the value of the UserName property comes back looking like this:

FABRIKAM\kenmyer

That’s fine if you want both the domain name (FABRIKAM) and the user name (kenmyer). But what if you want only the user name? How can you separate the user name from the domain name?

Believe it or not, this is pretty easy. With the UserName property the domain name and the user name will always be separated by a \. This, by the way, is true even if the user is logged on locally; in that case the computer name and the user name will be separated by a \:

ATL-WS-01\kenmyer

Because of that we can use the VBScript Split function to split the UserName into an array; all we have to do is tell the Split function to split the name at the \. That will give us an array consisting of two pieces: the domain name and the user name. All we have to do then is echo back the second piece – the user name – and we’re in business.

Confused? Let’s take a look at the script and then we’ll walk you through it:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery(“Select * From Win32_ComputerSystem”)

For Each objItem in colItems arrName = Split(objItem.UserName, “\”) Wscript.Echo “Domain: ” & arrName(0) Wscript.Echo “Name: ” & arrName(1) Next

We begin by connecting to the WMI service on the local computer. (Although we could just as easily connect to the WMI service on a remote computer. To do that just change the value of the variable strComputer to the name of the remote computer.) We then use the ExecQuery method to retrieve all instances of the Win32_ComputerSystem class (there will be only one such instance).

Next we set up a For Each loop to walk through all the instances of the Win32_ComputerSystem class. In the first script we showed you – the one that echoed both the domain name and the user name – all we did in this loop was echo the value of the UserName property. This time we’re going to do things a little different: we’re going to use the Split function to create an array out of the UserName property. This line of code splits the UserName value at the \ mark, and creates an array named arrName:

arrName = Split(objItem.UserName, “\”)

Incidentally, the Split function will split a string as many times as needed; that is, as many times as it finds the target character (in this case, the \). For example, suppose we had the following string:

element_1\element_2\element_3\element_4

If we run the Split function against this string our resulting array will actually have four elements:

element_1
element_2
element_3
element_4

With the UserName property we’ll end up with a two-element array, something like this:

FARBRIKAM
kenmyer

In an array, the first element (FABRIKAM) is always element 0; the second element (kenmyer) is always element 1. To echo back the domain name, we simply echo back the value of element 0; to echo back the user name, we simply echo back the value of element 1. That’s what we do here:

Wscript.Echo “Domain: ” & arrName(0)
Wscript.Echo “Name: ” & arrName(1)

That’s all we have to do: we can now echo back just the domain name, just the user name, or both. And best of all, our editor will quit bugging us about addressing basic scripting questions.

At least for awhile anyway.


0 comments

Discussion is closed.

Feedback usabilla icon