This is an attempt to answer my own questions asked almost 2 years ago. I am still using RDP on a daily basis, and have spent more time reading about the
First, let me answer the original question. According to its documentation, the
tsdiscon command does take a range of parameters, including
query session command through the command prompt shall reveal these two fields.
PS C:\WINDOWS\system32> query session
SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>rdp-tcp#84 Your_Username 1 Active
console 3 Conn
rdp-tcp 65536 Listen
Up to an hour before typing up this answer, I have been confused by where should one issue the
tsdiscon command: the original confusion in the question signifies a particular misunderstanding ==> the
tsdiscon command is not supposed to be issued from a local machine when this local machine is a personal computer. This is more true when I am the single user of this local personal computer. I bet the intended usage of
tsdiscon is for server admin to kick people off from their server :)
Still, I think it is worth the time to discuss how to properly get back from remote RDP sessions. For now, I am taking a AutoHotKey-approach that comes in two parts: 1. to get back from RDP session; and then 2. to kill the local session of RDP from the local machine.
Getting back temporarily from the remote RDP session
For now, I have devised the following shortcut to get me back from a RDP session. While keeping the identical script running both the local machine and the remote RDP-connected machine, pressing Ctrl + CapsLock (Ctrl first, then Capslock) shall "hide" the RDP session, and almost always restore keyboard focus + mouse focus back to the local machine.
; The following are AutoHotKey scripts.
#IfWinActive ahk_class TscShellContainerClass
; Make-shift script as suggested by: https://autohotkey.com/boards/viewtopic.php?t=25432
; May solve the awkward loss-of-focus when returning back from RDP
WinGetClass activeclass, A
WinGetTitle activetitle, A
MsgBox, 48, Warning, %activetitle% ahk_class %activeclass%, 0.666666
Simple solutions to "kill" RDP session(s)
Command Prompt/Powershell approach: terminate an active RDP session on the remote machine
As nicopowa has mentioned in this post,
tscon.exe 1 /dest:console shall terminate an active RDP session on the remote machine and leave the remote machine unlocked.
One extension is to create an alias for WSL, as:
alias rdp_stop='tscon.exe 1 /dest:console'. Then, calling
rdp_stop on the console of WSL running on the remote computer shall shut down that specific RDP session.
tscon.exe 1 /dest:console not only shuts down the RDP connection to the remote machine. It also "unlocks" the remote computer.
Solution: calling the
tsdiscon.exe executable alone on the RDP-connected remote computer, instead, terminates the RDP session and leave the remote machine unlocked. Then, the alias can be:
alias rdp_stop_leave_remote_machine_unlocked='tscon.exe 1 /dest:console'
AHK approach: terminate (all) local RDP sessions
As the Ctrl + CapsLock shortcut should be working 99% of the times, I then simplify the task as: kill the existing RDP session. Again, AutoHotKey comes handy, as I may have multiple RDP sessions to different machines running, and I shall only need to kill one of them.
WinClose, <Session 1: name_of_the_saved_RDP_config_file> - Remote Desktop Connection
WinClose, <Session 2: name_of_the_saved_RDP_config_file> - Remote Desktop Connection
One would need to carefully substitute the
<Session 1...> portion of the AHK script. It needs to match the Window-Title of the RDP session when it is active. I usually look it up using the following procedures:
- Open an RDP session in a Window, i.e. without having it span all active monitors
- Open "Windows Spy", an AHK-utility that reveals all identifiers for a "window": full set of identifiers include Window-Title, process_name and win_class_name.
PS: during my weekly home(-code-)improvement session, I headed out to solve the
tsdiscon issue again. With very similar query terms, I am happy to re-discover this old question. With a bit more careful reading of the documentation, it became apparent that I should not bet on one single command to handle all usages of mine. Thus come this pro-longed answer. Hope it can help people who play with RDP a lot.