This script works on a Windows 7 x64 PC and a Windows XP x86 PC, but not on a Windows XP X64 PC, and I don't know why.

strComputer = "."
Set objNetwork = CreateObject("WScript.Network")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
' Check if a user is logged in and if so, report back who it is
For Each objComputer in colComputer
If IsNull(objComputer.UserName) Then
' typically we'd run this ONLY when a user is already logged in, so this scenario shouldn't happen,
' but let's thrown in a Quit command just in case.
'WScript.Echo "Logged-on user: " & objComputer.UserName
' parse string to remove the domain name
myStr = objComputer.UserName
tokens = Split(myStr,"\")
WScript.Echo tokens(1)
End If
0 Comments   [ + ] Show Comments


Please log in to comment


Hey RonW,

I just tested this on an x64 xp machine and it returned the logged in user.

What are you using to run the script?
Answered 03/27/2012 by: dchristian
Red Belt

  • I'm using a program called VbsEdit and can step through the code. Also can double-click it to run it, and also tried calling it from a command line w/ cscript.exe. None of these return anything. Script runs without erroring, but I get no username. Have tried on multiple XP 64 PCs where I work, and same result.

    Stepping through the code, the line that's problematic is:
    If IsNull(objComputer.UserName) Then

    This is "IsNull" on my XP x64 PCs, but not on any of the others. Not sure why.
Please log in to comment
Answer this question or Comment on this question for clarity