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 8.00 €

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
Buy Windows Server product key from
10.80 € Find Out More
Subscribe
Notify of
guest
0 comments
Inline Feedbacks
View all comments