To set environment variables in Windows via the command line, you can use the
setx command. Here’s the basic syntax:
setx VARIABLE_NAME "variable_value"
VARIABLE_NAME with the name of the environment variable you want to set, and
"variable_value" with the desired value for that variable.
For example, to set the
MY_VARIABLE environment variable to
Hello World, you would run:
setx MY_VARIABLE "Hello World"
Please note the following important points:
- Changes Take Effect in New Command Prompt Windows: Changes made with
setxwill only take effect in new command prompt windows. They won’t affect the current command prompt window or any other programs you have running.
- System Variables vs. User Variables: By default,
setxsets the environment variable at the user level. If you want to set a system-wide environment variable, you need to add the
/Mflag. For example:
setx /M SYSTEM_VARIABLE "System Value"
- Special Characters: If your variable value contains special characters like quotes, you may need to escape them or use the appropriate syntax depending on the context.
- Path Variable: Setting the
PATHenvironment variable requires special handling because it’s a list of directories separated by semicolons. To add a new directory to the
PATH, you can use the following syntax:
setx PATH "%PATH%;C:\New\Directory" /M
C:\New\Directory to the existing
- Permissions: Running
setxrequires administrative privileges for system-wide changes.
Remember to close and reopen any command prompt windows after making changes to environment variables to see the effects.