How to Disconnect a Remote Desktop Protocol (RDP) Session Without Logging Off in Batch Script
Managing Remote Desktop Protocol (RDP) sessions efficiently is a common task for system administrators and power users. Sometimes, you need to disconnect from a remote session to release the RDP client window while keeping your applications and processes running on the server. Unlike logging off, which terminates all running programs, disconnecting simply closes the visual connection, leaving the user logged in and the session active.
In this guide, we will explore how to disconnect an RDP session using Batch Script, ensuring your workflow remains uninterrupted on the remote machine. We will cover standard disconnection methods and advanced techniques like sending a session to the console.
Understanding the Difference: Logoff vs. Disconnect
Before diving into the commands, it is crucial to understand the difference between logging off and disconnecting:
- Logoff (
logoffor Sign out): This closes all applications, ends the windows session, and disconnects the user. All unsaved work is lost. - Disconnect (
tsdiscon): This closes the remote desktop client connection but keeps the session alive on the server. Programs continue to run, and you can reconnect to the same session later.
Method 1: Disconnecting the Current Session
The simplest way to disconnect your current RDP session is by using the tsdiscon command. This command is built into Windows and is specifically designed for this purpose.
The tsdiscon Command
When you run tsdiscon without any arguments, it disconnects the session from which the command is executed.
Syntax:
tsdiscon
Example:
Create a batch file named disconnect_me.bat with the following content:
@echo off
echo Disconnecting current session...
timeout /t 2 >nul
tsdiscon
When you double-click this script inside your RDP session, your Remote Desktop window will close immediately, but your user account stays logged in on the remote server.
Method 2: Disconnecting a Specific Session
Sometimes you may need to disconnect a different session, or you might be running a script as an administrator and need to disconnect a specific user (e.g., a "stuck" session that isn't fully unresponsive but needs to be closed on the client side).
Step 1: Find the Session ID
First, you need to identify the Session ID of the connection you want to disconnect. You can use the query session or qwinsta command.
query session
Sample Output:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
console 1 Conn
>rdp-tcp#0 Administrator 2 Active
rdp-tcp 65536 Listen
In this example, the current RDP session (rdp-tcp#0) has an ID of 2.
Step 2: Disconnect by ID
Once you have the ID, use tsdiscon with the ID number.
Syntax:
tsdiscon <SessionID>
Example:
tsdiscon 2
Script to Automate Disconnection by Username
This script finds the session ID for a specific user and disconnects them.
@echo off
setlocal EnableDelayedExpansion
set "TARGET_USER=Administrator"
echo Searching for session ID of user: %TARGET_USER%
set "FOUND="
:: query session output columns:
:: SESSIONNAME USERNAME ID STATE ...
:: When a session has a SESSIONNAME (e.g., "rdp-tcp#0"), all columns are present.
:: When SESSIONNAME is blank (e.g., disconnected), columns shift left.
:: We grab enough tokens and test which one is the numeric ID.
for /f "skip=1 tokens=1-5" %%A in ('query session "%TARGET_USER%" 2^>nul') do (
:: Token C is the ID when SESSIONNAME is present
echo %%C | findstr /r "^[0-9][0-9]*$" >nul 2>&1
if !errorlevel! equ 0 (
:: Verify the state token (D) indicates an active session
echo %%D | findstr /i "Active" >nul 2>&1
if !errorlevel! equ 0 (
echo Found Active session ID: %%C
echo Disconnecting...
tsdiscon %%C
set "FOUND=1"
)
) else (
:: Token B is the ID when SESSIONNAME is blank
echo %%B | findstr /r "^[0-9][0-9]*$" >nul 2>&1
if !errorlevel! equ 0 (
echo %%C | findstr /i "Active" >nul 2>&1
if !errorlevel! equ 0 (
echo Found Active session ID: %%B
echo Disconnecting...
tsdiscon %%B
set "FOUND=1"
)
)
)
)
if not defined FOUND (
echo Session for %TARGET_USER% not found or not active.
)
endlocal
You generally need administrative privileges to disconnect sessions other than your own.
Method 3: Switching Session to Console (Advanced)
A common specialized use case for disconnecting is when you need to run GUI automation tests (like Selenium or AutoIt) on a remote server. RDP usually locks the screen when you disconnect, which breaks these tests.
To disconnect and leave the session unlocked (simulating a user staring at the physical monitor), you can redirect the RDP session to the physical "console".
Command:
tscon %sessionname% /dest:console
Note: This command usually requires Administrator privileges.
Usage: Create a batch file on the remote desktop desktop:
@echo off
echo Redirecting to console in 2 seconds...
timeout /t 2 >nul
:: Retrieve the current session's numeric ID for tscon
for /f "tokens=3" %%A in ('query session "%USERNAME%" 2^>nul ^| findstr /i "Active"') do (
set "CURR_ID=%%A"
)
if defined CURR_ID (
%windir%\System32\tscon.exe %CURR_ID% /dest:console
) else (
echo [ERROR] Could not determine the current session ID.
pause
)
When you run this (as Administrator), your RDP client will disconnect, but the session on the server attaches to the physical console screen, unlocked and running.
Common Mistakes and Best Practices
Mistake: Using logoff instead of tsdiscon
A common error is confusing logoff with disconnect.
Wrong:
:: This will kill your running apps!
logoff
Correct:
:: This keeps apps running
tsdiscon
Mistake: Trying to disconnect without rights
If you try to disconnect another user's session without being an Admin, you will get an "Access is denied" error. Always ensure you run your management scripts with elevated privileges when dealing with other users' sessions.
Summary
Disconnecting an RDP session via Batch Script is a powerful way to manage remote workflows. Whether you simply want to close the client window with tsdiscon or perform advanced tasks like redirecting to the console with tscon, these commands allow you to maintain persistent environments without keeping a connection open 24/7.