Scripting - How to disconnect Remote Desktop sessions?
Solution 1
You could try using the undocumented /sm
parameter for query session
(in a batch file) to sort things more easily:
FOR /f %%G IN ('query session /sm') DO tsdiscon %%G
Solution 2
Yes, using tsdiscon from a command line:
tsdiscon n
where the n should be replaced with the session id.
You can get the session number from
query session
Since you say you want to close all sessions on the local computer, I guess you will need to be careful about the order in which you do it (ie close your session last).
Solution 3
You can use the tsdiscon utility to disconnect sessions. If you use the "query sessions" command from a command-prompt, you can see the list of IDs and then issue a tsdiscon command for each one.
A looping construct like this should work
FOR /f %%G IN ('q.bat') DO tsdiscon %%G
where q.bat is
query session /sm | find "Active"
That will only disconnect remote sessions and ignore the console user.
Solution 4
You may want to check out Powershell Community Extensions. It includes Get, Stop and Disconnect TerminalSession cmdlets.
Solution 5
You can disconnect local or remote sessions with tsdiscon.
Disconnects a terminal session.
TSDISCON [sessionid | sessionname] [/SERVER:servername] [/V]
sessionid The ID of the session.
sessionname The name of the session.
/SERVER:servername Specifies the Terminal server (default is current).
/V Displays information about the actions performed.
Related videos on Youtube
Martin
Updated on September 17, 2022Comments
-
Martin over 1 year
I am looking for a way to close all active Remote Desktop sessions on a computer (local computer). Windows includes a couple of commands (rwinsta, qwinsta, etc.) to look at the active sessions, but I don't see how I could easily use the information (unless I parse the string...) to close all the sessions.
Is there a way in Powershell (or C#, Batch) to close all Remote Desktop sessions on a local computer?
Thanks, Martin
-
Kevin Kuphal almost 15 yearsI updated my answer to include a loop that will only disconnect active remote sessions, not the console
-
-
Martin almost 15 yearsWell... that's an idea. But I don't want to kill all the sessions. Only the remote desktop sessions.
-
Martin almost 15 yearsThat's a good beginning... but I want to disconnect only Remote Desktop sessions.