Windows Command Prompt Environment Variables
last modified July 14, 2025
Environment variables in Windows Command Prompt are named values that store system and user information. They affect how programs and scripts behave. Variables can contain paths, configuration settings, or temporary data. Both Windows and users can create and modify these variables.
System environment variables are predefined by Windows and remain consistent across all user sessions. User variables are specific to each user account. Temporary variables exist only for the current command prompt session. Variables are referenced by enclosing their names in percent signs (%VARNAME%).
Common system variables include %PATH% (executable search paths), %TEMP% (temporary files directory), and %USERPROFILE% (current user's home directory). These provide portable ways to reference system locations in scripts. Variables help create flexible scripts that work across different Windows installations.
This tutorial covers environment variable concepts, viewing and modifying variables, and practical examples of their usage. You'll learn to leverage variables for more powerful command-line scripting and system administration.
Viewing Environment Variables
The most basic operation with environment variables is viewing their values. Cmd provides several ways to inspect variables and their contents.
@echo off echo System Root: %SystemRoot% echo User Profile: %USERPROFILE% echo All variables: set
This script demonstrates different methods to view environment variables. It shows specific variables and lists all available variables.
echo System Root: %SystemRoot%
Displays the Windows installation directory stored in SystemRoot. This is a critical system variable used by many programs and scripts.
echo User Profile: %USERPROFILE%
Shows the path to the current user's profile directory. This variable helps create portable scripts that work across different user accounts.
set
Lists all environment variables for the current session. The output includes both system and user-defined variables with their current values.
C:\>viewvars.bat System Root: C:\Windows User Profile: C:\Users\username All variables: ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\username\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files ...
The output shows the specific variables first, followed by the complete list. Your actual values will differ based on your system configuration.
Creating and Modifying Variables
You can create new environment variables or modify existing ones using the set command. Changes can be temporary or permanent.
@echo off echo Creating temporary variable... set MYTEMP=Hello echo %MYTEMP% echo Modifying PATH... set PATH=%PATH%;C:\MyTools echo New PATH: %PATH%
This script demonstrates creating a temporary variable and modifying the system PATH variable. Changes made this way only affect the current session.
set MYTEMP=Hello
Creates a new environment variable named MYTEMP with value "Hello". This variable will only exist for the current command prompt session.
set PATH=%PATH%;C:\MyTools
Appends C:\MyTools to the existing PATH variable. The %PATH% reference gets the current value, and we add our new path separated by a semicolon.
C:\>modifyvars.bat Creating temporary variable... Hello Modifying PATH... New PATH: C:\Windows\system32;C:\Windows;C:\MyTools
The output shows our temporary variable and the modified PATH. Note that PATH changes revert when the command prompt closes.
Using Variables in Paths
Environment variables are commonly used to construct file paths dynamically. This makes scripts more portable across different systems and users.
@echo off echo Desktop location: %USERPROFILE%\Desktop echo System drivers: %SystemRoot%\System32\drivers mkdir "%USERPROFILE%\MyScripts" echo Created directory: %USERPROFILE%\MyScripts
This script shows how to use environment variables to reference common system locations when working with files and directories.
echo Desktop location: %USERPROFILE%\Desktop
Displays the path to the current user's desktop. Using %USERPROFILE% ensures the script works regardless of the actual username or profile location.
mkdir "%USERPROFILE%\MyScripts"
Creates a new directory called MyScripts in the user's profile folder. Quotes are used to handle paths containing spaces correctly.
C:\>pathvars.bat Desktop location: C:\Users\username\Desktop System drivers: C:\Windows\System32\drivers Created directory: C:\Users\username\MyScripts
The output demonstrates how variables resolve to actual system paths. The created directory will appear in your user profile folder.
Conditional Execution with Variables
Environment variables can control script flow through conditional statements. This allows creating flexible scripts that adapt to different environments.
@echo off if "%PROCESSOR_ARCHITECTURE%"=="AMD64" ( echo Running on 64-bit system ) else ( echo Running on 32-bit system ) if exist "%TEMP%\testfile.txt" ( echo Temporary file exists ) else ( echo Temporary file does not exist )
This script demonstrates using environment variables in conditional statements to make decisions based on system characteristics or file existence.
if "%PROCESSOR_ARCHITECTURE%"=="AMD64"
Checks whether the system is 64-bit by examining the PROCESSOR_ARCHITECTURE variable. This helps create architecture-specific script branches.
if exist "%TEMP%\testfile.txt"
Tests for the existence of a file in the temporary directory. The %TEMP% variable points to the system's designated temporary files location.
C:\>conditional.bat Running on 64-bit system Temporary file does not exist
The output depends on your system architecture and whether the test file exists. Your results may vary based on these factors.
Persistent Environment Variables
To make environment variable changes permanent, you need to use the setx command or modify them through System Properties. These changes persist across reboots.
@echo off echo Creating persistent variable... setx MYPERM "Persistent Value" echo Current value in this session: %MYPERM% echo Start a new command prompt to see the persistent value.
This script demonstrates creating a persistent environment variable that remains available in future command prompt sessions.
setx MYPERM "Persistent Value"
Creates a new user environment variable that persists after the command prompt closes. The variable will be available in all future sessions.
echo Current value in this session: %MYPERM%
Attempts to display the new variable's value. Note that setx doesn't affect the current session - only future ones.
C:\>persistent.bat Creating persistent variable... SUCCESS: Specified value was saved. Current value in this session: %MYPERM% Start a new command prompt to see the persistent value.
The output shows the variable was saved but isn't available in the current session. Open a new command prompt to verify the persistence.
Source
Microsoft SET command documentation
In this article, we have covered Windows Command Prompt environment variables. These techniques enable more flexible and portable scripting and system administration.