37

Environment is in domain, server is Windows Server 2003, workstations have Vista and XP installed.
I need the way to check remotely who is currently logged on workstation, preferably from some simple command line and without sysinternals or third party programs.

Thanks

Andrija
  • 569
  • 2
  • 5
  • 9

4 Answers4

47

This was the original source but that is now 404 :

They suggested using the (Windows Management Interface Command) WMIC which available on windows :

WMIC /NODE: xxx.xxx.xxx.xxx COMPUTERSYSTEM GET USERNAME 

Will return the username currently logged into xxx.xxx.xxx.xxx, or

WMIC /NODE: "workstation_name" COMPUTERSYSTEM GET USERNAME 

will return the username currently logged into "workstation_name"

UPDATE: This should working on Windows 10 too - if you are an admin on the remote machine.

Preet Sangha
  • 2,727
  • 1
  • 24
  • 25
  • When I run this command either remotely or locally I get only a single line of output "`UserName`" with nothing else listed, even though I'm logged-in to that machine. – Dai Feb 13 '17 at 21:44
  • 1
    @Dai Are you using XP or Vista or 2003? This question was for those os – Preet Sangha Feb 14 '17 at 00:53
17

Sorry, did not notice you do not want to use Sysinternals.
That is now a Microsoft technet tool, any specific reason to not use it?
I have preferred Sysinternals over other third party tools before Mark Russinovich moved into Microsoft.


The Microsoft Sysinternals Suite has a tool called Psloggedon,

psloggedon.exe -l

There is also NBTSTAT,

nbtstat -a NetBIOS-Computer-NAme
nik
  • 7,040
  • 2
  • 24
  • 30
  • 3
    sysinternals is just the business. I hope they paid Mark a ton of money to go there, currently they havn't stopped him doing the good work he was doing before and long may that continue. – gbjbaanb Jun 27 '09 at 13:44
  • @gbjbaanb, I am happy about that. Hope he keeps updating and adding to the suite. – nik Jun 27 '09 at 15:45
  • 1
    This worked wonders for me, whereas the `WMIC` in the accepted answer complained that RPC was not running. That's a new can of worms that I don't feel like opening right now, so I checked into psloggedon and I'm quite happy. – Mike S Sep 21 '15 at 13:29
12

I've used win32_loggedonuser, but ran into an issue where more than one domain user was returned, so it didn't work for my purposes. Instead I used (In powershell)

#Get Currently logged in user
$ExplorerProcess = gwmi win32_process | where name -Match explorer

if($ExplorerProcess.getowner().user.count -gt 1){
    $LoggedOnUser = $ExplorerProcess.getowner().user[0]
}

else{
    $LoggedOnUser = $ExplorerProcess.getowner().user
}

the if is because sometimes getowner will report more than one user for some reason, don't know why but in my case it was the same user so it wasn't an issue.

MDMoore313
  • 5,531
  • 6
  • 34
  • 73
  • 2
    The reason is that more than one user may be logged in. Fast user switching etc. was introduced years ago. I)t keeps the other user logged on. – TomTom Oct 18 '13 at 07:20
  • Good Point @TomTom, I forgot about that, I think [win32_loggedonuser] would also return anyone who's psexec'd or powershell'd in as well. – MDMoore313 Oct 18 '13 at 13:03
6

You can get this info from win32_loggedonuser.

From this page:

strComputer = "."   ' " use "." for local computer

Set objWMI = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!\\" _
              & strComputer & "\root\cimv2")

Set colSessions = objWMI.ExecQuery _
    ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")

If colSessions.Count = 0 Then
   Wscript.Echo "No interactive users found"
Else
   For Each objSession in colSessions
     If objSession.LogonType = 2 Then
       WScript.Echo "Logon type: Console"
     Else
       WScript.Echo "Logon type: RDP/Terminal Server"
     End If
     Set colList = objWMI.ExecQuery("Associators of " _
         & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
         & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )

     For Each objItem in colList
       WScript.Echo "User: " & objItem.Name
       WScript.Echo "FullName: " & objItem.FullName
       WScript.Echo "Domain: " & objItem.Domain
     Next
     Wscript.Echo "Session start time: " & objSession.StartTime
     WScript.Echo
   Next
End If
Zypher
  • 36,995
  • 5
  • 52
  • 95
HK_
  • 407
  • 2
  • 5