Log-off terminal services session remotely

There are times when you want to quickly log-off a number of terminal services sessions without having to log on to the server itself, perhaps because of the following error:

The terminal server has exceeded the maximum number of allowed connections.

Microsoft provide two useful command-line tools to view and terminate sessions, qwinsta (Query WINdoes STAtion) and rwinsta (Reset WINdows STAtion).

qwinsta is used to list sessions, simply use the “server” parameter to specify the server you’re interested in. See below for sample usage, replacing SERVER_NAME with the name of your server.

C:\>qwinsta /server:SERVER_NAME
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 ica-tcp#8         JBloggs1                  2  Active  wdica
                   JBloggs2                  3  Disc
 ica-tcp#1         JBloggs3                  4  Active  wdica
                   JBloggs4                  5  Disc
 ica-tcp#3         JBloggs5                  6  Active  wdica

Armed with either the sessionname or the id from the qwinsta you can now log-off the remote session using the rwinsta command:

c:\>rwinsta 3 /server:SERVER_NAME

If all goes well the user will be logged-off and the command will execute with visible output. If you don’t have access to the server you will likely see the following error:

Could not reset session ID 3, Error code 5
Error [5]:Access is denied.

If this happens make sure the account the command prompt is running under has suitable rights on the remote server. In an Active Directory environment this probably means just running the command prompt as a different user. If the server is not a member of the domain you may first need to access the server using a UNC path such as the c$ share.

Published by

Dave Hope

Dave is a Principal Software Analyst for a UK based retirement developer, in his spare time he enjoys digital photography and rock climbing.

Leave a Reply

Your email address will not be published. Required fields are marked *