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

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

  • Comments 8
  • Likes

 

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.


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • This appears to not function properly in Windows 7. Can someone confirm? I get .username = null

  • Hi all i am new vbscript. How to get User surname, given-name, user id, mail id.

    Thanks Advance.

  • The WMI call in this example does not work in Server 2008/Win7. Is there an updated class to query?

    I tried querying the logon sessions, but that returns a null value as well, even on the Win7 box I'm using to run the script. Using WMI explorer shows these values as null also.

    Thanks

  • There is a easier and faster way to get that value which works on win 7

    Set objNet = CreateObject("WScript.NetWork")

    and then use objNet.UserName, objNet.ComputerName and objNet.UserDomain to get the values.

  • Win7 RDP will give you null.

    WScript.NetWork is not equivalent - it gives owner of calling process e.g. SYSTEM.

  • problem with this for me is it still returns the user name of last user after theyve logged off.

  • Stoinov, your alternative will only work on local pc

  • for the most part you don't need a script to connect to the wmic service on a machine, local or remote. Open a cmd and type: wmic /node:computernamehere computersystem get username and it should return the value of the logged on user only.  Works for win7 and xp. Never tried it on vista... who uses it anyway? I have had times though were the returned user was UserName and not the actual person logged on but that's rare and I think it has something to do with the machine and not the actualy wmic service.