How to terminate remote windows PC session without locking screen

Local screen will be locked down if we log in the machine remotely, and the machine keeps locked down until someone log in this machine locally. This is for security reason, but not very convenient sometimes. Take the In-Home Streaming from Steam for example, if the machine is locked down, we can only see secure desktop in stead of the streaming game scenes, when we try to stream the game to other machine. Is there a way to terminate remote session without locking down the machine locally?

Possible Solutions

Usually, we could achieve that by using third-party software such as TeamViewer, but as a matter of fact, we don't need to install any software, all we need is a simple batch file

Writing a remote session-terminating batch

Let's create a batch file named disconnect.cmd, with the following content:

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

This batch first query session id of the current user, then disconnect it using tscon. In this way, windows will return the session to local screen, instead of locking down it. This batch requires administrator to run, so we must right click the file and choose 'Run as administrator', but we could make the batch auto elevated, based on our last post An auto elevated batch :

@ECHO OFF
NET SESSION 1>NUL 2>NUL
IF %ERRORLEVEL% NEQ 0 GOTO ELEVATE
GOTO ADMINTASKS

:ELEVATE
CD /d %~dp0
MSHTA "javascript: var shell = new ActiveXObject('shell.application'); shell.ShellExecute('%~nx0', '', '', 'runas', 1);close();"
EXIT

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

Then we can simply double click the batch file, the file will ask us to promote it, if it doesn't have the right, instead of showing error for permission denied.