Admin-Ahead Community

Windows => General Windows => Topic started by: Aby on March 21, 2014, 01:27:44 pm

Title: How to Log Off all Terminal Server Session Users using a batch file.
Post by: Aby on March 21, 2014, 01:27:44 pm
How to Log Off all Terminal Server Session Users using a batch file

In some occassions the administrators have to force log-off all user terminals, currently logged on to a windows server session. Below is a simple batch file that helps you out here. You can also run this command from command line. It would be easier for you if copy the code and save as a batch file.

Please follow below steps :

1. open a note pad

2. Copy the below code

---
query session >session.txt
for /f "skip=1 tokens=3," %%i in (session.txt) DO logoff %%i
del session.txt
---

3. Save the file as .bat as extension.

4.Click on the .bat file to see the result

Code Description:

Query Session creates a list of all sessions running on the Terminal Server, complete with Session ID numbers.
Within the batch file, this output is redirected to a text file. The FOR statement then parses each line of the text file, skipping the first line, and looking for the Session ID number found in the third column. It then places this variable into Logoff, resulting in that session being logged off.

-----