Skip to content

SET Windows Command Guide

The Windows set command is a powerful tool used to create, change, or display environment variables. With this command, users can control system settings efficiently, making it an essential component of the Windows command line interface.

SET Syntax:

Terminal window
set [option] [parameter]

Options:

OptionDescription
/ASpecifies that the string to the right of the equal sign is a numerical expression that is evaluated.
/PPrompts for confirmation before setting the environment variable.
/SModifies variables in the current command shell.

Parameters:

ParameterDescription
variableThe name of the environment variable to set.
valueThe value to assign to the variable.

SET Command Samples:

Set a Environment Variable

Terminal window
set MYVAR=myvalue

Sets the environment variable MYVAR to the value “myvalue”.

Set a Variable in a Batch File

Terminal window
set VAR1=12345

Sets the variable VAR1 to the value “12345” in a batch file.

Display All Environment Variables

Terminal window
set

Displays all environment variables with their current values.

Set a Variable Without Displaying

Terminal window
set /p VAR2=Enter a value:

Sets the variable VAR2 by prompting the user to enter a value without displaying it.

Set a Variable String with Spaces

Terminal window
set MYSTRING="Hello, World!"

Sets the variable MYSTRING to the string “Hello, World!” including spaces.

Set a System Variable

Terminal window
setx PATH "%PATH%;C:\NewFolder"

Sets a new folder to the system variable PATH, which includes the previous PATH value and the new folder.

Set Variables for an If-Else Condition

Terminal window
set USER=Admin
if "%USER%" == "Admin" (
echo Welcome,%USER%!
) else (
echo Access denied.
)

Demonstrates setting a variable and using it in an if-else condition to display a message based on the variable value.

SET FAQ:

How do I use set in Windows?

To use the set command in Windows, execute the following command:

Terminal window
set myVar=Hello

How can I display all variables in Windows CMD?

To display all variables in Windows CMD, use the following command:

Terminal window
set

How do I clear a specific variable in Windows CMD?

To clear a specific variable in Windows CMD, use the following command:

Terminal window
set myVar=

How can I display the value of a specific variable in Windows CMD?

To display the value of a specific variable in Windows CMD, use the following command:

Terminal window
echo %myVar%

How do I set an environment variable that persists after the CMD window is closed?

To set an environment variable that persists after the CMD window is closed, use the following command:

Terminal window
setx myVar Hello

How can I combine multiple set commands in a single line in Windows CMD?

To combine multiple set commands in a single line in Windows CMD, use the following command:

Terminal window
set var1=value1 && set var2=value2

Applications of the SET Command

  • Assigning a value to a variable
  • Displaying the values of variables
  • Setting environment variables
  • Displaying the current environment variables
  • Setting system variables