Skip to main content

How to Get the Session Name (Console, RDP) in Batch Script

Identifying the current session name is crucial for scripts that need to behave differently depending on whether they are running in an interactive console session or a remote desktop (RDP) session. For example, a script might only need to launch a GUI application if a user is actively logged in via RDP, or perform maintenance tasks only if running on the physical console.

In this guide, we will explore how to retrieve the current session name using environment variables and commands in Batch Script.

Using the %SESSIONNAME% Environment Variable

The most direct way to get the session name is through the built-in environment variable %SESSIONNAME%.

Values:

  • Console: The physical console session (or the session attached to the physical monitor).
  • RDP-Tcp#0, RDP-Tcp#1, etc.: A Remote Desktop session. The number indicates the specific connection ID.
  • (Empty): Sometimes this variable is not set in certain non-interactive contexts (like scheduled tasks running as SYSTEM), but usually it defaults to Console.

Example Script:

@echo off
echo Current Session Name: %SESSIONNAME%

if /i "%SESSIONNAME%"=="Console" (
echo You are running on the physical console.
) else if defined SESSIONNAME (
echo You seem to be in a remote session: %SESSIONNAME%
) else (
echo Session name is not available.
)
pause

Using query session or qwinsta

If you need more details about all sessions or want to verify the current session ID relative to others, use the query session command (or qwinsta). This command lists all sessions on the machine.

Command:

query session

Output:

SESSIONNAME USERNAME ID STATE TYPE DEVICE
services 0 Disc
>console Administrator 1 Active
rdp-tcp 65536 Listen

The > symbol indicates the current session from which the command was run.

Parsing query session Output

To get just the session name programmatically, you can parse the output of query session for the line starting with >.

@echo off
setlocal EnableDelayedExpansion

set "CurrentSession="

:: Parse query session to find the line starting with >
:: The > marker is at position 0, and the session name starts at position 1.
:: We read the entire line and extract the session name by trimming.
for /f "tokens=*" %%A in ('query session 2^>nul ^| findstr "^>"') do (
set "RawLine=%%A"
)

if not defined RawLine (
echo [ERROR] Could not determine the current session.
pause
exit /b 1
)

:: Remove the leading > character
set "RawLine=!RawLine:~1!"

:: Extract the first token (session name) from the trimmed line
for /f "tokens=1" %%S in ("!RawLine!") do (
set "CurrentSession=%%S"
)

echo Detected Session via query: !CurrentSession!

if /i "!CurrentSession!"=="console" (
echo Confirmed: Console Session
) else (
echo Confirmed: Remote Session (!CurrentSession!^)
)

pause
tip

Note: The %SESSIONNAME% environment variable is generally faster and easier to use than parsing query session. Use parsing only if you suspect the environment variable is missing or tampered with.

Common Use Cases

1. Preventing RDP Execution

If you want a script to only run on the physical machine (e.g., a kiosk setup script) and exit if accessed via RDP:

@echo off
if not defined SESSIONNAME (
echo Error: Session name is not available.
pause
exit /b 1
)

if /i not "%SESSIONNAME%"=="Console" (
echo Error: This script must be run from the physical console.
echo Current session: %SESSIONNAME%
pause
exit /b 1
)

echo Running console-only tasks...

2. Logging Session Details

When logging script execution, it is helpful to record where it was run from.

@echo off
setlocal

set "LOGFILE=script.log"

if defined SESSIONNAME (
set "SESS=%SESSIONNAME%"
) else (
set "SESS=Unknown"
)

echo Script started at %TIME% on %DATE% by %USERNAME% in session %SESS% >> "%LOGFILE%"

Summary

Checking the %SESSIONNAME% environment variable is the standard way to determine if a script is running in a Console or RDP session. This simple check allows you to create context-aware scripts that adapt their behavior based on how the user is connected to the system.