Identify what servers a user is logged on to with powershell

I am guilty to disconnect from a server and forget all about it and perhaps, months later, I find myself logged on that server. I built a quick script that helps me with this every now and then. Or perhaps you’re onto somebody else šŸ™‚ Let’s see how to identify what servers a user is logged on to with powershell!

First things first, you will need a list of servers (this will work on all clients as long as you have rights to query all of the machines).
I prefer to grab the list from Active Directory (you will need AD-RSAT tools with Powershell unless you run this on a Domain Controller) with the following command:

If you have already a simple list of hostnames stored in a text file, you can import it with:

Let’s see the whole script, it’s commented so that you can understand, but I’ll go through it:

So what’s happening is very simple:

  1. First IĀ stated the username (just the username and not the domain etc).
  2. The csvOutput is the file that will be stored in C:\ which will contain a list of Hostames and Session IDs. In the script I also initialised it so that the first line will haveĀ Hostname,Session ID.
  3. I then grabbed the list of servers that are not disabled from Active Directory.
  4. In the foreach cycle, I went through each server in the Server List. For each server the script will:
    • Try to ping it (with just one request to make it faster).
    • Grab the session ID when the user matches. This is the most important line of the script. Also note that I used cmd /c so that I could use “2>NUL”. This is a nice workaround to avoid the script to outputĀ No User exists for *. This output is displayed every time quser hits a server with no users logged on to it.
    • Check if the Session ID exists, it makes sure it doesn’t contain “rdp” and that does not contain “console”. In my opinion we should avoid to grab a list of active sessions, especially if you want to use the csv file at a later stage to kill these sessions. Perhaps you’re logged off and you’re going to kill an active session from another user, losing all of his/herĀ work!
    • If the Session ID respected the above criteria, then write to console something like User.Name is logged on SERVER01 with ID:5 and then it’ll also write SERVER01,5 in the CSV file.

That’s it! This is how you identify what servers a user is logged on to with powershell.

Note: Somebody might suggest a different approach which is grabbing allĀ explorer.exe on the server and then check its owner in another cycle. I do not like this approach as you might be in an environment with servers running without a GUI!

5 thoughts on “Identify what servers a user is logged on to with powershell

  1. I really need this script but its not working… can you help?
    I get: Test-Connection : Cannot validate argument on parameter “ComputerName” The argument is null or empty

    1. Hi Erik, looks like you have an empty variable there. So Test-Connection might be complaining about that. Did you verify that you have a list of Computers?
      If you run just $serverList=(Get-ADComputer -Filter (‘(OperatingSystem -Like “*SERVER*”) -AND (Enabled -Eq “True”)’) | select-object Name).Name and then type $serverList followed by an ENTER, you should get a list of servers. If you’re importing a list manually, there might be another issue. Please explain further after testing the above.

  2. You are correct, nothing when I pull from AD and then type $serverlist. SOO I tried using a txt file, still the same error but when I type $serverlist it DOES show me the list of servers from my txt file. Thank you so much for responding, I am an amateur with PS.. this script will be a lifesaver if I can get it to work!

    1. Ok. I’ve sent you an email to the account you’ve stated in the comment form. We can post a solution once we understand what’s wrong. In case the email address is wrong or you haven’t got it, comment again down here stating the correct address in the form (nobody but myself will see that).

Leave a Reply

Your email address will not be published. Required fields are marked *