Is it possible (through group policy, etc) to automatically restore the console session to a Windows 8.1 PC after a remote desktop session disconnects?

I'm aware that you can run the command "tscon 1 /dest:console" during the RDP session to disconnect yourself and reconnect the console session, but is it possible to have this happen automatically?

3 Answers 3


Create a batch file with the following contents, named something like restore_console.bat:

@echo off
set LOG_FILENAME=%TEMP%\restore_console_log.txt
echo Script executed at %TIME% > %LOG_FILENAME%
echo qwinsta: >> %LOG_FILENAME%
qwinsta >> %LOG_FILENAME%

echo Checking for pending connection... >> %LOG_FILENAME%
for /f %%i in ('qwinsta ^| findstr /r /C:"^ [ ]*[0-9][0-9]*  Disc"') do (
echo Pending connection detected, finishing. >> %LOG_FILENAME%
goto end

echo Checking for disconnection... >> %LOG_FILENAME%
for /f "tokens=2" %%i in ('qwinsta ^| findstr /r /I /C:"^ [ ]*[^ ][^ ]* [ ]*[0-9][0-9]*  Disc"') do (
echo Redirecting session id %%i >> %LOG_FILENAME%
tscon %%i /dest:console /v >> %LOG_FILENAME%
goto end


In Task Scheduler, create a new task, with the following settings:

  • General -> Run whether user is logged on or not, Run with Highest privileges.
  • Triggers -> New -> On disconnect from user session, Any user, Connection from remote computer
  • Actions -> New -> Start a program -> Program/script: <your batch file>
  • Everything else default.

Notes regarding implementation:

  • this works by parsing the qwinsta output through a findstr regex, i.e. extracting the ID from line 3 here:

     SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
    >services                                    0  Disc                        
                       ######                    2  Disc                        
     console                                     7  Conn                        
     #############...                        65536  Listen                      
     rdp-tcp                                 65537  Listen                      
  • the middle block is necessary because for some reason the schedule task executes on connection as well as disconnection. When this happens, the output will be of the form:

     SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE 
    >services                                    0  Disc                        
                       ######                    2  Disc                        
                                                 3  Disc                        
     console                                     8  Conn                        
     #############...                        65536  Listen                      
     rdp-tcp                                 65537  Listen                      

    Therefore we look for lines of the pattern from line 4.

  • it dumps logging information to %TEMP%\restore_console_log.txt, which is not necessary, but useful if the script doesn't work. Without the logging it would be only a few lines.

This worked for me on a single Windows 8.1 machine - I don't know if it could be rolled out globally.

  • Thanks very much! This script didn't work exactly as intended on my Windows 10 machine but it gave me enough of an idea of how to accomplish what I wanted.
    – SofaKng
    Oct 2, 2015 at 15:21
  • @SofaKng I'm having a little trouble adapting this to a Windows 10 machine. (I had it working on 8.) Would you mind sharing what you had to change? (The .bat file works if I invoke it from admin console, but fails to start as a task. The task is set to run with highest privileges.) Oct 21, 2016 at 16:49
  • @willmuphyscode I had the same issue on windows 10. I'll post an answer below detailing what changed/how to fix. Jan 26, 2017 at 21:34

@Kim's answer is great. Unfortunately for us, it appears Microsoft broke starting a scheduled task on remote connect/disconnect in Windows 10. No matter what I did, I couldn't get any task, much less this one, to run when connecting or disconnecting via remote desktop.

Fortunately triggering on generic events still works. This question about where events are logged for remote desktop disconnects gave me a starting point, and trial and error yielded the right one.

Change the trigger to be "On an event" from Log "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational", Source "TerminalServices-LocalSessionManager", Event ID "24". (I originally recommended 40 - both seem to work, but 40 is a bit noisier, possibly occurring multiple times on a disconnect. If 24 doesn't work, try 40 and let us know.) See screenshot below.

enter image description here

  • I think this may have been broken in the just-released Fall Creators Update of Windows 10, ver. 1709.... I've yet to get a task to run for either event id 24 or 40 in this version of windows. Oct 30, 2017 at 21:26
  • The fall creators update change is that a task configured this way will no longer run if you have "do not store password" checked. I was using this option, and removing it (so that credentials are stored) fixed it. Oct 30, 2017 at 21:46

I know this is a bit old, but it should be possible to write a quick Windows service which monitors the connections and restores the console session when it sees that the RDP is now in a disconnected state. Slightly less work would be to use a batch file utilizing srvany to run it as a service, just have it monitor the values returned from qwinsta.

I'd recommend doing the batch/srvany as a state machine. First, the batch file monitors for an RDP connection and continues monitoring until it sees one. Then it moves into the next state where it waits for that RDP session to be done. When it detects the session has been disconnected (not logged off) it moves to the final state which is to restore that session to the console. Finally, it moves back into the original state of monitoring for an RDP session.

I'm not saying it would be easy, but it should be possible. Mainly I wanted to offer up the suggestion because your "tscon [sessionID] /dest:console" saved me a lot of headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.