How to use the command 'set' (with examples)
- Windows
- December 25, 2023
The ‘set’ command is used to display or set environment variables for the current instance of CMD in Windows. It allows the user to view and modify the values of variables that are used by various processes running on the system.
Use case 1: List all current environment variables
Code:
set
Motivation:
Sometimes it is important to know all the environment variables that are currently set for a particular system. This can be useful for troubleshooting or understanding the system’s configuration.
Explanation:
Running the ‘set’ command without any arguments will display a list of all the current environment variables and their corresponding values. These variables include both system-wide variables and user-defined variables.
Example output:
...
TEMP=C:\Users\User\AppData\Local\Temp
TMP=C:\Users\User\AppData\Local\Temp
USERNAME=User
USERPROFILE=C:\Users\User
...
Use case 2: Set an environment variable to a specific value
Code:
set name=value
Motivation:
In some cases, it is necessary to define a new environment variable or modify the value of an existing one. This could be required for customizing specific applications or setting up configurations.
Explanation:
To set an environment variable, use the ‘set’ command followed by the name of the variable and its desired value. The variable name and value are separated by an equal (=) sign.
Example:
set MY_VAR=Hello World
Use case 3: List environment variables starting with the specified string
Code:
set name
Motivation:
In situations where there are numerous environment variables defined, it can be helpful to filter the list and display only the variables that start with a specific string. This can aid in locating a specific environment variable quickly.
Explanation:
By running the ‘set’ command followed by the name of the variable as an argument, the command will display all the environment variables that start with the specified string. The output will include the names and values of these variables.
Example:
set TEMP
Output:
TEMP=C:\Users\User\AppData\Local\Temp
TMP=C:\Users\User\AppData\Local\Temp
Use case 4: Prompt the user for a value for the specified variable
Code:
set /p name=prompt_string
Motivation:
Often, it is desired to prompt the user to enter a value for a specific environment variable during a script or batch file execution. This allows for user input and customization within the command prompt.
Explanation:
Using the ‘/p’ option with the ‘set’ command, followed by the name of the variable and a prompt string, the command will prompt the user to enter a value for the specified variable. The prompt string is displayed to the user to provide context for the input.
Example:
set /p MY_VAR=Enter a value for MY_VAR:
Output:
Enter a value for MY_VAR: Hello World
Conclusion:
The ‘set’ command in Windows CMD is a useful tool for managing environment variables. Whether it is listing all variables, setting specific values, filtering based on names, or prompting for user input, the ‘set’ command provides the flexibility to control and customize the environment variables for a given instance of CMD.