Skip to main content

How to Get a List of Environment Variables in Batch Script

Environment variables are a foundational part of the Windows operating system, acting as a shared memory space where the OS and applications can store and retrieve configuration settings. For a batch scripter, knowing how to access this list is essential for debugging, finding the location of installed programs, and writing scripts that can adapt to different machine configurations.

This guide will teach you how to use the SET command to display a full list of all available environment variables and, more importantly, how to filter this list with FINDSTR to find the specific variable you need.

What are Environment Variables?

Environment variables are key-value pairs that define your command-line session's environment. They tell your scripts where to find important system folders (%SystemRoot%), where the current user's profile is (%USERPROFILE%), and which folders to search for executables (%PATH%).

The Core Command to List All Variables: SET

The SET command is the primary tool for managing variables. While you use it to create variables (SET Var=Value), running it with no arguments will display an alphabetized list of every environment variable currently known to your command prompt session.

C:\> SET

Output (Abridged)

APPDATA=C:\Users\Admin\AppData\Roaming
COMPUTERNAME=MY-PC
ComSpec=C:\WINDOWS\system32\cmd.exe
...
Path=C:\WINDOWS\system32;C:\WINDOWS;...
...
USERNAME=Admin
USERPROFILE=C:\Users\Admin
windir=C:\WINDOWS

Filtering the List to Find Specific Variables

The full list from SET can be very long. The most practical use is to pipe this list to another command to find what you're looking for. The FINDSTR utility is perfect for this.

Let's find all variables related to the "user": SET | FINDSTR /I "user" where

  • |: The pipe operator sends the output of SET as input to FINDSTR.
  • /I: Makes the search case-Insensitive.

Output:

ALLUSERSPROFILE=C:\ProgramData
USERNAME=Admin
USERPROFILE=C:\Users\Admin

This is an incredibly fast way to check if a variable exists or to see its current value without scrolling through the entire list.

Viewing System vs. User Variables in the GUI

For a more organized view, you can see the permanently stored System and User variables in the GUI.

  1. Press Win + R, type SystemPropertiesAdvanced.exe, and press Enter.
  2. Click the "Environment Variables..." button.

This dialog box clearly separates the variables for the current user from the system-wide variables. The SET command in a script shows you the final, combined result of both.

Key System and User Variables to Know

This is a short list of some of the most useful variables for scripting.

VariableDescription
%USERPROFILE%The path to the current user's profile folder (e.g., C:\Users\Admin).
%USERNAME%The name of the current user (e.g., Admin).
%APPDATA%The path to the user's AppData\Roaming folder.
%TEMP% / %TMP%The path to the user's temporary files folder.
%SystemRoot%The Windows directory (e.g., C:\Windows).
%windir%Typically the same as %SystemRoot%.
%ProgramFiles%The path to the main Program Files folder.
%ProgramFiles(x86)%On 64-bit systems, the path to the 32-bit Program Files folder.
%COMPUTERNAME%The name of the computer.
%PATH%A semicolon-separated list of directories where Windows looks for executables.

Common Pitfalls: The "Invisible" Dynamic Variables

Some of the most useful variables in batch scripting will not appear in the SET command's list. This is because they are dynamic, meaning their value is calculated at the moment you request it.

These include:

  • %CD%: The current directory.
  • %DATE% & %TIME%: The current date and time.
  • %RANDOM%: A random number.
  • %ERRORLEVEL%: The exit code of the last command.

You just have to know that these exist; you cannot discover them by running SET.

Practical Example: A Script to Find JAVA_HOME

This is a common real-world task for a system administrator. The script checks if the JAVA_HOME environment variable has been set, which is a prerequisite for many Java-based applications.

@ECHO OFF
SETLOCAL
SET "JavaPath="

ECHO --- Checking for JAVA_HOME Environment Variable ---
ECHO.

REM Filter the SET output for a line starting with "JAVA_HOME="
REM The /B switch matches the pattern at the beginning of the line.
SET | FINDSTR /I /B "JAVA_HOME=" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] JAVA_HOME is defined.

REM Bonus: Get the actual value
FOR /F "tokens=1,* delims==" %%A IN ('SET ^| FINDSTR /I /B "JAVA_HOME="') DO (
SET "JavaPath=%%B"
)
ECHO Path is: %JavaPath%

) ELSE (
ECHO [FAILURE] JAVA_HOME is not set. Java applications may fail.
)

ENDLOCAL

Conclusion

The SET command is your window into the script's environment, providing a complete list of the variables that define the session.

Key takeaways:

  • Run SET with no arguments to get a full, alphabetized list of all defined variables.
  • Pipe the output to FINDSTR to quickly find specific variables (e.g., SET | FINDSTR /I "name").
  • Remember that some of the most useful variables (%CD%, %DATE%, etc.) are dynamic and will not appear in this list.

By using SET for discovery and debugging, you can write more aware and adaptable scripts.