How to Log Off Remote Desktop Sessions Using Command Line



Scenario:

You cannot connect to a Windows Server box (either 2008 or 2012) via remote desktop. Whenever you try to connect, you encounter either of the below errors.

Error 1:
The system cannot log you on due to the following error:
Not enough resources are available to complete this operation.
Please try again or consult your system administrator.

Error 2:
This computer can't connect to the remote computer.
Try connecting again. If the problem continues, contact the owner of the remote computer or your network administrator.

Solution:

Since RDP is not possible, you need to connect to the problematic Windows server via command line and log off unwanted RDP sessions.

Two steps are needed for this:
  1. Query the current remote sessions using command line
  2. Log off unwanted remote sessions 

Query the current remote sessions using command line

To query and list the sessions on the remote server, you can use either QUSER or QWINSTA

a. Using quser command
Syntax:
quser /server:hostname

hostname = hostname of the server you want to list the logged on users
If no server name is specified, quser will display logged users on current server (localhost).

Example:
C:\Users\Administrator>quser /server:phserver01
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME     LOGON TIME
 quezonm               rdp-tcp#0          2  Active   1:00   8/6/2016 12:05 AM
 magellanf             rdp-tcp#1          3  Active     29   8/6/2016 12:34 AM
 rizalj                rdp-tcp#2          4  Active     12   8/6/2016 1:16 AM

b. Using qwinsta command (has more options than quser, run qwinsta /? for more options)
Syntax:
qwinsta /server:hostname

Example:
C:\Users\Administrator>qwinsta /server:phserver01
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 console                                     1  Conn
                   administrator             2  Disc
                   quezonm                   3  Disc
 rdp-tcp#1         delpilarm                 4  Active  rdpwd
 rdp-tcp                                 65536  Listen


Log off unwanted remote sessions

To log off the unwanted remote sessions, you can use either LOGOFF or RWINSTA

a. Using logoff command

Syntax:
logoff [session ID] /server:hostname /v

Example:
If you want to log off quezonm from quser sample above,
C:\Users\Administrator>logoff 2 /server:phserver01 /v
Logging off session ID 2

b. Using rwinsta command (has more options than logoff, run rwinsta /? for more options)

Syntax:
rwinsta [session ID] /server:hostname

Example:
If you want to log off quezonm from qwinsta sample above,
C:\Users\Administrator>rwinsta 3 /server:phserver01 /v
Resetting session ID 3
Session ID 3 has been reset

Another way of logging off RDP sessions on a remote computer is by using a powerful tool called PsExec. But this will be discussed on a different topic.


No comments