Microsoft - Scripting - Batchfiles - Scripts - RDLogOffAllUsers.cmd

REM RDLogOffAllUsers.cmd
REM Logs off all users from a Remote Desktop host.
REM Based on https://support.microsoft.com/en-us/help/259436/
REM 2017-12-11, v1.

REM Use output of 'query session'.
REM Skip the first 3 lines (header and the services and console lines).
REM Store colums 2 and 3.
REM Logoff the disconnected sessions (where column 3 (%%j) = Disc).
REM Logoff the active sessions (where column 3 is not Disc but a number, 
REM and column 3 is not Listen).
 
for /f "skip=3 tokens=2,3," %%i in ('"%SYSTEMROOT%\system32\query.exe" session') DO (
	IF "%%j" EQU "Disc" "%SYSTEMROOT%\system32\logoff.exe" %%i
	IF NOT "%%j" EQU "Disc" IF NOT "%%j" EQU "Listen" "%SYSTEMROOT%\system32\logoff.exe" %%j
)