Session not foundThis usually happens when the username contains spaces, for example:
USER NAME
In such cases, the query user command fails to correctly locate the session, preventing the script from executing as expected.
To work around the issue caused by spaces in the username, use the wildcard variable %User*Name% instead of %USERNAME%.
This ensures the user session is identified correctly, even when the name contains spaces.
for /f "skip=1 tokens=3" %%s in ('query user %User*Name%') do (%windir%\System32\tscon.exe %%s /dest:console)
This script performs the following:
query user searches for the session using a wildcarded username.
tscon disconnects the session and redirects it to the console.
If your disconnection script fails with a "Session not found" error and the username contains spaces, simply replace %USERNAME% with %User*Name%.
This small change ensures your script functions correctly regardless of the username format