Blog

When administering terminal servers running Windows, it is often necessary to terminate a local user’s session on a remote terminal server. Using standard methods, such as connecting to the terminal server and ending a user’s session via Task Manager, can be time-consuming, especially when managing multiple servers.

To simplify this task, you can use an alternative approach—creating a convenient command-line script to automatically terminate user sessions on remote servers.

You can always buy genuine product keys for Windows in our catalog from $13.30

Using logoff and qwinsta Utilities

To terminate a remote user’s session, you can use the command-line utility logoff, designed for ending sessions on terminal servers. However, this utility requires the session ID, which can be obtained using another utility, qwinsta.

The qwinsta utility lists all active sessions on a server along with their session IDs. By combining qwinsta and logoff in a single script, you can automate the process of terminating user sessions on terminal servers.

Example Script for Terminating User Sessions on Terminal Servers

Below is an example .bat script that terminates remote user sessions on specified servers. Before using the script, replace TERMSERV1 and TERMSERV2 with the names of your terminal servers.


@echo off
setlocal

set servers=TERMSERV1 TERMSERV2

for %%s in (%servers%) do (
qwinsta /server:%%s | for /f "tokens=3" %%i in ('findstr /I "%username%"') do logoff /server:%%s %%i /v
)

How the Script Works

1. @echo off: Suppresses the display of commands on the screen for a cleaner and more user-friendly interface.

2. setlocal: Initiates a local environment variable session to prevent changes from affecting the global environment.

3. set servers=TERMSERV1 TERMSERV2: Defines the list of servers on which the script will run. Replace TERMSERV1 and TERMSERV2 with your server names.

4. The for loop iterates through each server in the servers list and executes the qwinsta command to retrieve the list of sessions.

5. The findstr command searches for the line containing the username (%username%) and extracts the session ID.

6. The logoff command terminates the user session using the retrieved session ID.

Testing and Using the Script

This script was tested on Windows XP, Windows 7, and Windows 10 when connecting to Windows Server 2008 R2. It works correctly and significantly speeds up the process of terminating remote user sessions on terminal servers.

The script is particularly useful for system administrators who regularly manage terminal servers and manually terminate user sessions, a time-consuming task. With this script, you can automate the process and reduce the number of routine operations.

banner for Windows Server
Windows Server license from
$10.00 ·
Picking your discount
Promo code ·
Promo code expired. A new one in .
Buy valid for

"Personal discount from 5% to 12%" — promotion rules

  1. Organizer — SoftComputers LLP, website softcomputers.net. The promotion runs indefinitely; the organizer may end it at any time by removing the block from the website.
  2. Participation is free. To take part, enter your email and agree to the processing of personal data and to receive emails.
  3. Each participant receives a promo code for a 5% to 12% discount on any product in the catalog. The amount is chosen at random.
  4. The promo code is single-use, tied to the email you entered, valid for 7 days and applied in the cart at checkout. No minimum order amount. Cannot be combined with other promo codes.
  5. One participant (email / device) can receive one promo code per 7 days; a new code is issued after the previous one expires.
  6. The promo code cannot be exchanged for money. The organizer may void a code in case of abuse.
Subscribe
Notify of
guest
0 comments
Newest
Oldest