Save as batch and it should work. You can run it via scheduled task with the SYSTEM account.
REM Set variables
SET SOURCE=%~dp0
SET SOURCE=%SOURCE:~0,-1%
cd "%SOURCE%"
query session >session.txt
for /f "skip=2 tokens=3," %%i in (session.txt) DO logoff %%i
del session.txt