Environment variables are variables that are defined in the operating system to configure certain parameters such as the path to search for a command or executable, folder for temporary files, application-specific options, etc,. There are two types of environment variables - user environment variables and system environment variables. User environment variables are specfic to each user that it is set for, while system environment variables are available to everyone on that computer.
In this article you will learn how to
- View Environment variables
- Add / Modify Environment variables
- Add / Modify / Remove PATH variable
- Manage environment variables from Advanced System Settings
View Environment Variables
The SET command is used to view environment variables from the command line. To view all environment variables,
- Open command prompt - click the search icon and type cmd in the search bar. Then click on the Command Prompt from the results.
On the Command Prompt, type set and press Enter.
C:\> set ALLUSERSPROFILE=C:\ProgramData CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files
To view any specific variable, the command is
set
followed by that variable name.C:\> set windir windir=C:\WINDOWS
Add or Modify Environment Variable
Use the setx command to add a new environment variable permanently or to modify an existing environment variable.
If you want to add or modify a environment variable temporarily to the current command window only, then the command to use is set.
setx variableName value or set variableName=value
Example
c:\> setx TESTVAR someValue SUCCESS: Specified value was saved.
Now, open a new command prompt and run the set command to verify that the new environment variable is created.
c:\> Set testvar TESTVAR=someValue
Adding System Environment Variables
By default, the setx
command adds the variables as user variable in the local environment. To add a variable to the system environment use the /m parameter. You also need run the command prompt as an administrator.
c:\> setx TESTVAR someValue /m SUCCESS: Specified value was saved.
If the command windows is not run in administrator mode then you will get the following error
ERROR: Access to the registry path is denied.
The variables in the system environment are available to all users of the computer.
Difference between SET and SETX
The SET command is used to view, create and modify environment variables temporarily for the current command window. The variable that you add using SET are not available on any future command windows.
SETX command adds the variables permanently. The variables that are added using SETX are not available on the current command window but will be available on all future windows.
You cannot use setx
to remove a environment variable but you can use set
to remove a variable from the current command window.
Setting PATH variable
The PATH variable defines the search path for executable files. You can set the PATH variable from the command line or GUI.
View current PATH
To view the current path you can simply run the path command or echo %PATH%
command.
c:\> path PATH=C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;
Add path to current PATH
To add new path to the exiting path, use the SET or SETX command.
For example, to add the directory c:\NewFolder
to existing path, the command is
c:\> set PATH=c:\NewFolder;%PATH%
Modify an existing path
The SET and SETX commands can be also used to modify an existing path. The syntax for this command is
set PATH=%PATH:current_path=new_path%
For example, to change c:\NewFolder
in the current path to c:\AnotherFolder
, the command is
set PATH=%PATH:c:\NewFolder;=c:\AnotherFolder;%
Remove path from current PATH
The syntax for removing a folder path from the current PATH is same as modifying an existing path explained above, except that you just have to omit the new path.
set PATH=%PATH:path_to_remove;=%
For example, to remove c:\AnotherFolder;
from the existing path, the command is
set PATH=%PATH:c:\AnotherFolder;=%
Manage Environment variables from Advanced System Settings
You could also manage environment variables from Advanced System Settings in Windows. Type env
in the search box and select Edit environment variables for your account. This will open Environment Settings dialog from where you can add, edit and delete system and user environment variables.