Scripting - How to disconnect Remote Desktop sessions?
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
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).
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.
You may want to check out Powershell Community Extensions. It includes Get, Stop and Disconnect TerminalSession cmdlets.