Command Prompt Variables Tutorial
last modified July 15, 2025
Variables in Command Prompt are essential for storing and manipulating data. They can hold strings, numbers, paths, and other information. Variables enable dynamic behavior in batch scripts and command-line operations. Understanding variables is crucial for effective Windows automation.
Command Prompt supports two main variable types: environment variables and local script variables. Environment variables persist across sessions and are system-wide. Local variables exist only within a script or command session. Both types use similar syntax but have different scopes.
Variables are referenced by enclosing their names in percent signs (%). They can be created, modified, and deleted during a session. System environment variables are predefined by Windows. User-defined variables provide flexibility for custom scripts and configurations.
This tutorial covers variable creation, modification, and practical usage. We'll explore basic operations, string manipulation, arithmetic, and scripting examples. These concepts form the foundation for advanced batch programming and system administration.
Basic Variable Operations
This example demonstrates creating, displaying, and modifying variables. Basic operations form the foundation for more complex variable usage.
@echo off set MESSAGE=Hello there! echo Variable value: %MESSAGE% set MESSAGE=Hi there! echo Modified value: %MESSAGE% set MESSAGE=
In this script, we create a variable named MESSAGE
, display its
value, modify it, and then clears it. The @echo off
command
suppresses command echoing.
set MESSAGE=Hello there!
The set
command creates a new variable named MESSAGE
with the value "Hello there!". The set command is used for both creation and
modification of variables.
echo Variable value: %MESSAGE%
Displays the current value of MESSAGE
. Percent signs around the
variable name indicate it should be expanded to its value.
set MESSAGE=New Value
Updates MESSAGE
with a new value. Variables can be changed as often
as needed during a script's execution.
set MESSAGE=
Deletes the MESSAGE
variable by setting it to nothing. This clears
the variable from memory, freeing resources.
C:\Users\Jano>variables.bat Variable value: Hello there! Modified value: Hi there!
The output shows the variable's initial and modified values. The deletion operation produces no visible output.
Environment Variables
Environment variables provide system information and configuration settings. They are available across all Command Prompt sessions.
@echo off echo System root: %SystemRoot% echo User profile: %USERPROFILE% echo Path: %PATH% set TEMP_VAR=Temporary echo Temp var: %TEMP_VAR%
This script demonstrates accessing system environment variables and creating temporary ones. System variables are always available.
echo System root: %SystemRoot%
The line displays the Windows installation directory path.
SystemRoot
is a predefined environment variable available on all
Windows systems.
echo User profile: %USERPROFILE%
We show the current user's profile directory path. This variable helps create user-specific paths in scripts.
echo Path: %PATH%
We list directories where Windows searches for executables. The
PATH
variable contains multiple paths separated by semicolons.
set TEMP_VAR=Temporary
The set
command creates a temporary environment variable. Such
variables exist only for the current Command Prompt session.
User Input Variables
Variables can capture user input for interactive scripts. This enables dynamic behavior based on runtime input.
@echo off set /p username=Enter your name: echo Hello, %username%! set /p num1=Enter first number: set /p num2=Enter second number: set /a sum=%num1% + %num2% echo Sum: %sum%
This script demonstrates capturing user input and performing calculations. The /p switch enables prompt-style input.
set /p username=Enter your name:
It prompts the user to enter their name and stores it in username
.
The /p switch makes set wait for input instead of assigning directly.
echo Hello, %username%!
We display a personalized greeting using the captured input. User input variables make scripts interactive and flexible.
set /a sum=%num1% + %num2%
The line performs arithmetic addition of two numbers. The /a switch indicates arithmetic operation rather than string assignment.
C:\Users\Jano>input.bat Enter your name: Jan Hello, Jan! Enter first number: 5 Enter second number: 7 Sum: 12
When run, the script interacts with the user and performs calculations. Input values determine the script's output.
String Manipulation
Variables can store and manipulate strings. Command Prompt provides several string operations through variable expansion.
@echo off set text=The quick brown fox setlocal enabledelayedexpansion set /a len=0 :loop if not "!text:~%len%,1!"=="" ( set /a len+=1 goto loop ) echo Original: %text% echo Substring: %text:~4,5% echo Length: %len% letters set text=%text:fox=dog% echo Replaced: %text%
This script demonstrates substring extraction, length calculation, and string replacement. String operations use special syntax.
setlocal enabledelayedexpansion
This enables delayed variable expansion. It allows us to reference variables
whose values are updated within a code block (like inside a loop or if
statement) during runtime. In our case, it allows us to use the
!text!
syntax to access the variable text
even when
it is modified within the loop.
set /a len=0 :loop if not "!text:~%len%,1!"=="" ( set /a len+=1 goto loop )
This code calculates the length of the string by iterating through each character until it reaches the end. The loop continues until no character is left.
echo Substring: %text:~4,5%
We extracts 5 characters starting at position 4 (0-based index). The ~ operator enables substring operations on variables.
set text=%text:fox=dog% echo Replaced: %text%
Here we replace all occurrences of "fox" with "dog" in the variable. Replacement operations use the :search=replace syntax.
Local vs Global Variables
Variables can have different scopes in batch scripts. Understanding scope prevents unintended variable interactions.
@echo off set GLOBAL_VAR=Available everywhere echo Before setlocal: %GLOBAL_VAR% setlocal set LOCAL_VAR=Local only echo Inside setlocal: %LOCAL_VAR% endlocal echo After endlocal: %LOCAL_VAR% echo Global still exists: %GLOBAL_VAR%
This script demonstrates variable scoping with setlocal and endlocal. Local variables disappear after endlocal.
setlocal
The setlocal
command begins a local scope where variable changes
are temporary. Commands after setlocal
can modify variables without
global effect.
set LOCAL_VAR=Local only
We create a variable that exists only within the local scope. Such variables are
automatically cleaned up at endlocal
.
endlocal
The line ends the local scope and discards all variable changes made since
setlocal
. Global variables remain unaffected.
Source
This tutorial covered essential Command Prompt variable concepts. Master these fundamentals to create powerful batch scripts and automate tasks.