How to Disconnect and Disable an RDP User After a Set Time in Windows Server
Sometimes on a Windows Remote Desktop (RDS / Terminal) server you need to grant an external contractor, accountant, or manager temporary RDP access for a couple of hours, then automatically log off their session and disable the user account – so you never forget to lock the door at the end of the day.

You can handle this in two ways: a simple admin script that enables a disabled account for N hours and locks it back up, or built-in Group Policy RDP session timeouts for regular users.
Method 1. Script to Enable a Temporary RDP User for 2 Hours (.cmd and PowerShell)
If the account stays disabled by default (active:no) and you only turn it on when requested, run this batch script as Administrator: it enables the account, waits for the timer, forcibly logs off the user’s RDP session, and disables the account again.
Batch script version (temp_rdp.cmd, set to 7200 seconds = 2 hours for user Alex_manager):
@echo off
set "USER_NAME=Alex_manager"
set "WAIT_SEC=7200"
net user "%USER_NAME%" /active:yes
echo User %USER_NAME% enabled for %WAIT_SEC% seconds...
timeout /t %WAIT_SEC% /nobreak >nul
for /f "tokens=1,2,3" %%a in ('query session "%USER_NAME%" 2^>nul') do (
if /i "%%a"=="%USER_NAME%" logoff %%b
if /i "%%b"=="%USER_NAME%" logoff %%c
)
net user "%USER_NAME%" /active:no
echo User %USER_NAME% logged off and disabled.
Why does the for loop check both %%a and %%b? In query session output, an active RDP session lists SESSIONNAME (rdp-tcp#0) in column 1, USERNAME in column 2, and the numeric session ID in column 3. If the user already closed the RDP window with the X button, the session sits in a Disconnected (Disc) state with an empty first column – shifting USERNAME into %%a and the numeric ID into %%b. Checking both columns guarantees a clean logoff either way.
Here is the equivalent PowerShell version:
$user = "Alex_manager"
Enable-LocalUser -Name $user
Start-Sleep -Seconds 7200
$lines = query session $user 2>$null
foreach ($l in $lines) {
if ($l -match "\s+$user\s+(\d+)\s+") { logoff $matches[1] }
}
Disable-LocalUser -Name $user
Method 2. Built-in RDP Session Time Limits via Group Policy
If you do not want to disable the user account itself and simply want Windows Server to close idle or abandoned RDP sessions automatically:
- Press
Win+R, typegpedit.msc, and press Enter. - Navigate to:
Computer Configuration → Administrative Templates → Windows Components → Remote Desktop Services → Remote Desktop Session Host → Session Time Limits. - Configure the three core policies:
- Set time limit for disconnected sessions – how long a session stays alive after the user closes the RDP window (e.g., 15 or 30 minutes).
- Set time limit for active but idle Remote Desktop Services sessions – idle timeout with no keyboard or mouse input (e.g., 1–2 hours).
- Set time limit for active Remote Desktop Services sessions – maximum total duration of a session regardless of activity.
- Enable “End session when time limits are reached” so Windows actually performs a full
logoffrather than merely disconnecting the session into the background.
net user Alex_manager /times:M-F,09:00-18:00.For everyday staff accounts, Group Policy session limits in gpedit.msc are ideal for keeping abandoned RDP sessions from eating server RAM, while the net user /active:no script is perfect for one-off vendor or contractor access.