I am trying to get currently logged in users who has active session.
In the task manager, the list is crystal clear that there are two user sessions and one is active.
I want to query the same via Powershell.
I tried few of the available commands
Get-WmiObject Win32_LoggedOnUser | Select Antecedent -Unique
which lists lot more users than I can even see [domain joined computer]
I am looking for the query which can give results exactly like Task Manager.
Best Answer
The issue with
Get-WmiObject Win32_LoggedOnUser | Select Antecedent -Unique
is that it shows all sessions even those that have been closed since the last time the computer rebooted. The easiest way to poll sessions is unfortunately using the old executable query.exe.You can convert the output of query.exe to objects using a bit of regex:
Which will give you output like this:
Taking it a lot further in to a function:
and now you can do things like:
Get-ADComputer -Filter {Name -like "SERVER*"} | Convert-QueryToObjects | ? {$_.SessionState -eq 'Active'}