19

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?

glenviewjeff
  • 1,724
  • 4
  • 18
  • 34

5 Answers5

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

glenviewjeff
  • 1,724
  • 4
  • 18
  • 34
  • very welll done :) – J-D Jun 12 '15 at 03:43
  • since we stumbled upon that because of this [issue](https://superuser.com/questions/1480232/windows-10-v1903-suspends-microsoft-outlook-when-windows-remote-desktop-user-dis/1480363#1480363): 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 – rominator007 Oct 04 '19 at 12:42
9

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
Kim
  • 286
  • 3
  • 6
  • 7
    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](http://i.stack.imgur.com/YZAQo.png). – Helder Magalhães Nov 06 '15 at 17:50
  • I had to make this shortcut run as administrator for it to work. Right click > Properties > Advanced – James Esh Jun 07 '18 at 14:32
2

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.

Simon D
  • 6,421
  • 2
  • 16
  • 14
0

Step 1: Copy this script into a text file, for eg: rdp-disconnect.cmd.

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "$sessionid=((quser $env:USERNAME | select -Skip 1) -split '\s+')[2]; tscon $sessionid /dest:console" 2> rdp-disconnect-errors.log

Step 2: Create a shortcut to this file, for eg: rdp-disconnect.lnk.

Step 3: Setup this shortcut to run as admin.

-> right click shortcut link -> properties -> advanced -> run as administrator -> ok

Step 4: Double click this file when you need to disconnect from the server.

Benefits of this script:

  1. Very simple to setup.
  2. No need to figure out the session Id yourself.
  3. No need to pass the password.
  4. Logs the errors to a .log file.

Reference: https://stackoverflow.com/a/53507397/8644294

Ash K
  • 111
  • 4
0

I couldn't get Simon D's batch file command to work. This is what worked for me on Windows 7 x64:

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