I was recently tasked with locating all servers on our network and query for users that were currently logged onto each server, either through a terminal session or logged on via console session. This got me thinking of what ways are available to make this happen. When it was all said and done, I came up with 4 ways to do this.

The first method is to use the Win32_ComputerSystem and grab the UserName property. The thing that you keep in mind with is that this will only return the user that is logged on using a console session, meaning that they are locally logged onto the machine, not logged on via remote desktop.

The second method involves another WMI query that will work for both console sessions and remote sessions. This query looks at the Win32_Processclass and then performs a query to look for all of the explore.exe process, which is the user shell for each user that is logged into the server. Using this query, you can perform a wmi search and then create a custom object to hold the data. I used an advanced function I wrote to perform the query. This gets the job done and shows you who is logged into the machine, but it doesn’t really give you a lot of information to work with.

The third method is made using the query sessions command line, which is available in Vista and above OS’s and on systems running Terminal Servers. Just using this command line will return a string value which does list out a nice amount of information sessiontype, username, active state of the session, etc…:

query session /server:"boe-laptop"

This is nice and all, but I would rather return an object that I can sort or export into a csv or something else. So with that I went and created this advanced function to parse the data and make into a more usable object:

So now when I run this, I have my custom object that can be ran on multiple machines and list out much more information than my previous function.

The fourth and final way that I found to do this was using a freely available Terminal Services module, built by Shay Levi to query for user sessions. As you can see from the output, it works rather nicely.

Import-Module PSTerminalServices
Get-TSSession -ComputerName "dc1"

As you can see, there are a variety of ways to gather information on user sessions on local and remote machines. Some are very basic, but will work from any workstation/server, while others contain more information, but may only be available on certain systems.

You could probably use [adsisearcher] (if you wanted to avoid using a dependency such as the ActiveDirectory module) to search for the account and then pull the full name from the results of the search.

Hi..
when I am running the script under third method, I am getting below error… Please help
The term ‘query’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spel
ing of the name, or if a path was included, verify that the path is correct and try again.
At line:38 char:26
+ $sessions = query <<<< session /server:$c
+ CategoryInfo : ObjectNotFound: (query:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

You can use the WMI classes Win32_LogonSession and the associated Win32_LoggedOnUser to query logged on users! This gives more Informations.
I have developed a full blown function for that.
See: http://poshcode.org/4304
unfortunately I did not found a use case for the session IDs😦
If you use Quser.exe (or Query User) you get back a session ID, which you can use with LogOff.exe to log off a specific user. With that I have extended ArPoshhttp://blog.richprescott.com/2011/07/arposh-client-system-administration.html

Nice function that you uploaded to poshcode. The only thing that I would offer up is that you are using Begin and Process blocks in your function, but do not have any parameters which allow for pipeline input. I would recommend adding pipeline input for your computername parameter so it can be more robust. This link will show you what I mean: https://learn-powershell.net/2013/05/07/tips-on-implementing-pipeline-support/

Thanks! Glad you like the post! The “” in the $temp = “” | Select Computer,SessionName, Username, Id, State, Type, Device is just creating an empty collection for me to use later on in the script. If you look at the contents of $temp after creating the collection, it will appear as:

One more option with using your Get-WMIComputerSessions is to determine how long the explorer.exe process has been active for each user, which tells you how long the session has been active. Another process I typically check for when querying sessions is logon.scr, which tells me whether the session is actively being used or not, and if not, how long the user has been idle.

What OS are you running? From a command prompt, do you get anything when you type query and hit enter? If running an XP machine, most likely you will not be able to use this function as query.exe is a required piece of the function.