"Session Not Found" Error When Using a Startup Script to Disconnect User Sessions

"Session Not Found" Error When Using a Startup Script to Disconnect User Sessions

Issue Description

When using a startup or logout script to disconnect user sessions on Windows, some users encounter the following error:
WarningSession not found

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


Solution

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.

Corrected Script Example
Quotefor /f "skip=1 tokens=3" %%s in ('query user %User*Name%') do (%windir%\System32\tscon.exe %%s /dest:console)

This script performs the following:

  1. query user searches for the session using a wildcarded username.

  2. tscon disconnects the session and redirects it to the console.


Notes

The for /f "skip=1 tokens=3" syntax is used to extract the session ID from the query user output.
Using %User*Name% provides better compatibility for usernames with spaces or special characters.

Conclusion

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

    • Related Articles

    • Keeping your remote session active when using Runner

      In some cases where a VM is used to run the automation, keeping the remote session active is necessary to avoid problems with the graphical interface, such as the OSError: screen grab failed error. This error is usually thrown when trying to perform ...
    • Error Running Edge Browser via Orchestrator: How to Fix It?

      Issue During the execution of robots using the Microsoft Edge browser through the orchestrator, a specific issue was identified: the browser did not start correctly, whereas when running locally, the flow executed normally. The error presented was: ...
    • Resolving SSL certificate verification issues when running commands using 'pip'

      Access blocks are quite common in corporate environments. As a result, you may encounter problems when trying to use BotCity tools and Python resources on a machine in your company's environment. One problem that may occur in these scenarios is the ...
    • Error Running Wizard.exe: How to Fix It?

      Issue When attempting to start the installation, configuration, or authentication process of the BotCity Runner using the wizard in .exe format, you may encounter difficulties opening the file. This error usually occurs due to company security ...
    • How to fix Python environment preparation failed error

      A very common error when executing automations through the Runner is Python Environment Preparation Failed. As the name suggests, the Runner fails when trying to prepare the Python environment for the robot's execution. This error occurs in different ...