How to Remove RDP Sessions Using CMD and PowerShell

Introduction In IT administration, managing Remote Desktop Protocol (RDP) sessions is essential to maintain system security and performance. Whether you need to log off a disconnected session or forcefully remove an active session, using Command Prompt (CMD) or PowerShell can streamline the process. This guide will walk you through the methods to list, disconnect, and remove RDP sessions.


Step 1: List Active RDP Sessions Before removing a session, you need to check which users are currently logged in. Run the following command in CMD:

query session /server:localhost

This will display all active and disconnected sessions along with their IDs.

Alternatively, in PowerShell, use:

qwinsta /server:localhost

Both commands provide the session ID required to log off a user.


Step 2: Log Off an RDP Session Once you have the session ID, you can log off a user session using CMD:

logoff <SessionID> /server:localhost

Example:

logoff 2 /server:localhost

This will terminate session ID 2.

In PowerShell, use:

rwinsta <SessionID>

Example:

rwinsta 2

This will remove session ID 2 from the system.


Step 3: Forcefully Remove a Stuck RDP Session If a session does not close properly, use this CMD command:

taskkill /F /IM mstsc.exe

This will forcefully terminate all remote desktop connections.

In PowerShell, use:

Stop-Process -Name mstsc -Force

This achieves the same result.


Conclusion Managing RDP sessions efficiently helps prevent resource exhaustion and unauthorized access. By using CMD or PowerShell, IT admins can quickly remove inactive or unresponsive RDP sessions, ensuring smooth operations. Bookmark these commands for future reference!

error: Content is protected !!