How can I restore a remote desktop session to the local console?

38,189

Solution 1

  1. Create a desktop shortcut by right clicking on the desktop and selecting new, then select shortcut.
  2. In the text field enter:

    %windir%\System32\tscon.exe 0 /dest:console (See below)

  3. Right click the newly created shortcut, click properties.
  4. Click the shortcut tab, and click the Advanced button.
  5. Check the "Run as administrator" box and click OK.

If this doesn't work, try changing the number zero (tscon.exe 0 /dest...) in step 2 to the number one, and if it doesn't work, keep incrementing it until your remote desktop is released.

Alternately, open up a shell with start menu, run, cmd. Type qwinsta Enter, and look for the ID of the session that is in the active state (it will have a > character at the start of its session name). That's the number you need to use in step 2.

When you want to restore the console desktop, just double click on the shortcut and allow the administrator access.

Solution 2

Here's a version which avoids the dependency on GNU tools. It uses findstr, which is shipped with Windows.

for /f %%i in ('qwinsta ^| findstr /C:">rdp-tcp#"') do set RDP_SESSION=%%i
:: Strip the >
set RDP_SESSION=%RDP_SESSION:>=%
tscon %RDP_SESSION% /dest:console

Solution 3

glenviewjeff's answer got me most of the way there, but the session id is not always 1. If you try to disconnect the listening or console session like this you'll get an "Error 7045" - requested session access is denied, or if the session id doesn't exist a SessionID not found error.

I made a small batch file to pull out the current session. As I did this on Windows XP I needed to qwinsta rather than query session to figure out the current ID. This batch file uses unix command line utilities, I use Gnu on Windows (https://github.com/bmatzelle/gow/downloads) to have access to these. It pulls out the current session by searching for a ">" sign and then reassigns it back to the console session.

for /f %%i in ('qwinsta ^| grep "^>" ^| awk "{print $4}"') do set VAR=%%i
tscon %var% /dest:console

I needed this for a machine that is connected to a Fujitsu IX500 scanner, the scanner only scans if the screen is not on the user name / signon selection screen in Windows which is what you get when you log off or disconnect a session normally. As the machine runs without a screen I want to be able to connect via rdp, but if I did that I couldn't use the hardware scan button until I logged in manually or restarted. The batch file above solves this problem.

Share:
38,189

Related videos on Youtube

glenviewjeff
Author by

glenviewjeff

Updated on September 18, 2022

Comments

  • glenviewjeff
    glenviewjeff over 1 year

    After I am finished with a remote desktop connection, I'd like to be able to restore the remote desktop to the local console so that the user doesn't have to re-enter the password and log back in.

    How can I create a desktop shortcut to do this?

  • J-D
    J-D almost 9 years
    very welll done :)
  • Helder Magalhães
    Helder Magalhães over 8 years
    Here's my version of, optimized to a one-liner in order to be shortcut-friendly: %windir%\system32\cmd.exe /c "for /F "tokens=1 delims=^> " %i in ('""%windir%\system32\qwinsta.exe" | "%windir%\system32\find.exe" /I "^>rdp-tcp#""') do "%windir%\system32\tscon.exe" %i /dest:console" It replaces > environment variable replacement with additional for parsing trickery, as well as adds cmd options (for the shortcut). When combined with a proper icon (I used the log off icon from Shell32.dll), it gets similar to this.
  • James Esh
    James Esh almost 6 years
    I had to make this shortcut run as administrator for it to work. Right click > Properties > Advanced
  • rominator007
    rominator007 over 4 years
    since we stumbled upon that because of this issue: keep in mind that when using the work-around with tscon that the session stays open without lock! that means that anyone that has a connection to the console (for example via virtual machine manager console or vsphere) can use the open session even from another user without logging in. So in the used script there should also be a session lcok