The following code should help you (use strComputer="." For the local computer or strComputer="MachineName" ):
strComputer = "." Set objWMI = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colSessions = objWMI.ExecQuery _ ("Select * from Win32_LogonSession Where LogonType = 10") If colSessions.Count = 0 Then Wscript.Echo "No interactive users found" Else WScript.Echo "RDP Sessions:" For Each objSession in colSessions Set colList = objWMI.ExecQuery("Associators of " _ & "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _ & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) For Each objItem in colList WScript.Echo "Username: " & objItem.Name & " FullName: " & objItem.FullName Next Next End If
The original code is here:
How to show registered users? (Tek-Tips Forums)
This worked with Windows 2003, I can not guarantee any guarantees regarding a later version.
source share