How to List All Environment Variables in Batch Script
Environment variables are a core component of the Windows operating system. They are a set of dynamic named values that store system-wide settings (like the location of the Windows directory) and user-specific preferences (like the path to the user's profile). Batch scripts rely heavily on these variables to find common locations, configure behavior, and access system information.
This guide will teach you how to list all currently defined environment variables using the simple SET command. You will learn how to filter this list to find specific variables and understand the difference between system, user, and volatile variables.
What are Environment Variables?
Environment variables are key-value pairs that are available to every command-line process. They are a fundamental way that the operating system and applications share configuration information. They come in three main types:
- System Variables: Apply to every user on the computer (e.g.,
%windir%,%ProgramFiles%). Require administrator rights to change. - User Variables: Apply only to the currently logged-in user (e.g.,
%USERPROFILE%,%TEMP%). - Volatile Variables: Temporary variables that exist only in the current
cmd.exesession, created with theSETcommand (e.g.,%MyVar%).
Core Command: SET
The SET command is a multi-purpose tool. While it's used to create or modify variables (SET MyVar=Hello), when run with no arguments, its primary function is to display all environment variables that are currently defined in the session.
Syntax: SET
Basic Example: Displaying All Variables
Running the SET command by itself will produce a long, alphabetized list of every variable the current command prompt session is aware of.
C:\> SET
Output (truncated for brevity): The output includes system, user, and any session-specific variables.
ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Admin\AppData\Roaming
COMPUTERNAME=MY-PC
ComSpec=C:\WINDOWS\system32\cmd.exe
HOMEDRIVE=C:
HOMEPATH=\Users\Admin
NUMBER_OF_PROCESSORS=8
OS=Windows_NT
Path=C:\WINDOWS\system32;C:\WINDOWS;...
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.JS;
PROCESSOR_ARCHITECTURE=AMD64
ProgramFiles=C:\Program Files
ProgramFiles(x86)=C:\Program Files (x86)
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\Users\Admin\AppData\Local\Temp
TMP=C:\Users\Admin\AppData\Local\Temp
USERNAME=Admin
USERPROFILE=C:\Users\Admin
windir=C:\WINDOWS
...
Filtering the List to Find Specific Variables
The full list can be overwhelming. If you are looking for variables related to a specific topic (e.g., the processor or a user's path), you can pipe the output of SET to the FINDSTR command to filter it.
The following command will find all variables whose name starts with "PROCESSOR", ignoring case.
SET | FINDSTR /I /B "PROCESSOR"
/I: Ignore case./B: Match pattern at the Beginning of the line.
Output:
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=9e0a
Key System Variables You Should Know
While the SET command shows dozens of variables, a few are used constantly in batch scripting.
| Variable | Description |
|---|---|
%CD% | The Current Directory. |
%~dp0 | The drive and path of the batch script itself. |
%DATE%, %TIME% | The current date and time. |
%ERRORLEVEL% | The exit code of the last command. |
%RANDOM% | A random number between 0 and 32767. |
%USERPROFILE% | The path to the current user's profile folder (e.g., C:\Users\Admin). |
%TEMP% or %TMP% | The path to the user's temporary files folder. |
%windir% | The Windows directory (e.g., C:\Windows). |
%SystemRoot% | Usually the same as %windir%. |
%ProgramFiles% | The path to the Program Files folder (e.g., C:\Program Files). |
%PATH% | A list of directories where cmd.exe looks for executables. |
Common Pitfalls and How to Solve Them
- Variable Not Found: If you try to
ECHO %NonExistentVar%, it will simply print the literal string%NonExistentVar%. TheSETcommand is the best way to see the correct spelling of a variable you're looking for. - Dynamic vs. Static Variables: Some variables, like
%DATE%and%RANDOM%, are dynamic and their value is generated when they are requested. They do not appear in the output of theSETcommand because they aren't "stored" in the same way as other variables. You just have to know they exist. - Output Overload: The list from
SETcan be very long. Solution: Use the| MOREpipe to display the output one page at a time (SET | MORE) or pipe it toFINDSTRto filter for what you need.
Practical Example: A Script to Display Key System Info
This script uses the SET command to list all variables but also explicitly echoes a curated list of the most important system variables for a quick diagnostic report.
@ECHO OFF
ECHO --- Key System and User Variables ---
ECHO.
ECHO Username: %USERNAME%
ECHO User Profile: %USERPROFILE%
ECHO Temp Folder: %TEMP%
ECHO.
ECHO Computer Name: %COMPUTERNAME%
ECHO OS: %OS%
ECHO System Root: %SystemRoot%
ECHO Program Files: %ProgramFiles%
ECHO Processor Arch: %PROCESSOR_ARCHITECTURE%
ECHO.
ECHO ---------------------------------------
ECHO.
ECHO Press any key to see the full list of all environment variables...
PAUSE > NUL
CLS
ECHO --- Full Environment Variable List ---
ECHO.
SET
PAUSE
Conclusion
The SET command is the simplest and most direct way to get a complete picture of the environment your batch script is running in.
- Running
SETwith no arguments lists all defined environment variables. - This list is invaluable for debugging and discovering what information is available to your script.
- You can pipe the output to
FINDSTRto easily search for variables related to a specific name (e.g.,SET | FINDSTR /I "USER").
By familiarizing yourself with the output of SET and memorizing the key system variables, you can write more powerful, flexible, and system-aware batch scripts.