10

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

Martin
  • 233
  • 1
  • 3
  • 9

12 Answers12

7

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).

Rob Levine
  • 182
  • 7
5

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
Adam Brand
  • 6,057
  • 2
  • 28
  • 40
4

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.

Kevin Kuphal
  • 9,064
  • 1
  • 34
  • 41
2

You may want to check out Powershell Community Extensions. It includes Get, Stop and Disconnect TerminalSession cmdlets.

Chad Miller
  • 1,091
  • 8
  • 11
1

I found TSDISCON does not remove "Disc"onnected sessions.

I replaced TSDISCON with logoff.

1

You can use the PSTerminalServices PowerShell module:

http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2010/02/22/psterminalservices-powershell-module-for-terminal-services.aspx

Get-TSSession -ComputerName server1 -State Active | Disconnect-TSSession -WhatIf

Shay Levy
  • 929
  • 1
  • 5
  • 5
1

I know the OP is quite old now, but this should do what you're after (and incase anyone else is still looking for something to do the same, I thought I would reply anyway).

Similarly you could use query/reset session inplace of q/rwinsta...

FOR /F %%A IN (computers.txt) DO (
    FOR /f "tokens=2" %%i IN ('qwinsta /SERVER:%%A ^| find /i "disc"') DO ECHO %%i | rwinsta %%i /SERVER:%%A /V
)
Carly
  • 11
  • 1
1

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.
JasonMArcher
  • 125
  • 4
1

FOR /F %A IN (c:\scripts\ps\computers.txt) DO (FOR /f "tokens=2" %i IN ('qwinsta /SERVER:%A ^| find /i "disc"') **DO if "%i" NEQ "0" ECHO %i** | rwinsta %i /SERVER:%A /V)

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
greg
  • 11
  • 1
0

Even the question is very old there are hardly any solutions to all versions without qwinsta / query / tsdiscon, namely all Windows home versions.

However there is an easy powershell version to disconnect a session:

$code = @'
[DllImport("wtsapi32.dll")]
static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);

[DllImport("wtsapi32.dll")]
static extern void WTSCloseServer(IntPtr hServer);

[DllImport("Wtsapi32.dll")]
static extern bool WTSDisconnectSession(System.IntPtr hServer, int SessionId, bool bWait);

public static void DisconnectSession (String ServerName, int SessionId)
{
    IntPtr serverHandle = WTSOpenServer(ServerName);
    WTSDisconnectSession(serverHandle, SessionId, true);
    WTSCloseServer(serverHandle);
}
'@

$tstType=Add-Type -name Test -MemberDefinition $code -PassThru
#$tstType::DisconnectSession("localhost", 2)

A session id can be obtained e.g. with getProcesss. To disconnect (not logoff!) a session use $tstType::DisconnectSession("localhost", 2).

GMA
  • 1
0

The TSDiscon command will disconnect terminal services (RDP) sessions. Used on its own will disconect the current session, you can specify the session ID of the session you want to disconnect, you can also supply the server name to disconnect from.

Full details can be found here.

Sam Cogan
  • 38,158
  • 6
  • 77
  • 113
0

You didn't specify if you want to do this with out shutting down or restarting the computer. But if you don't mind a restart or shutdown you can just call:

shutdown -r -f -m \\computer_to_restart

This will actually force a restart of the computer.

Nick Berardi
  • 566
  • 3
  • 7
  • 11
  • Well... that's an idea. But I don't want to kill all the sessions. Only the remote desktop sessions. – Martin Jul 13 '09 at 18:43