Logoff a user from multiple servers with powershell

This article is related to the article I recently wrote on how to identify what servers a user is logged on to with powershell. Actually we can re-use all of that script and just implement the logoff to it.Ā We have a couple of ways to logoff a user from multiple server with pwoershell, based on the script located in the article I posted above:

  1. Utilise the .csv file, from the previous script, in a new script and run through it.
  2. Modify the script to add the log off functionality.

Both Options are really simple to follow and are both helpful since the first one will allow you to have a quick look through the list whilst the second one can be used when you’re sure you want to log everybody off!

1 – Utilise the .csv file, from the previous script, in a new script and run through it.

Remember that the script returned a .csv file composed of two columns per row: the hostname and the session ID of the user we want to log off. Since the previous script has done all of the work, all we need to do will be:


Easy! Pay attention though! If this is not your own account you’re working with, I would suggest you to add an extra check to make sure you’re logging off the right username. You could do something like this (we’re ignoring the Session ID the previous script caught):


Note: with the second script, you can use any list to perform this. You do not need the session ID as the script will find it by itself!

2 – Modify the script to add the log off functionality.

This is exactly the same script of the above article, it just has an extra line that will log off the user as well.

This is pretty much it on how to logoff a user from multiple servers with powershell.


Read More

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.


Read More