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.

If you haven’t read yet the article posted at the beginning of this post, I totally recommend you to do it so that you can better understand the script above.

Leave a Reply

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