Environment Variables hold data that is used by Operating System Environment. They give us information about the environment in which program runs. There are two types of environment variables:
- System Environment Variables : These contain information specific to System resources and are set by the Operating System or by the drivers when the programs are installed . For example, windir variable will have the path where windows is installed.
- User Environment Variables : These contain information for a particular user account. For example , when you install Python/JAVA in your system, you set PATH variable so that the commands can be run without specifying the the file-path every single time.
Environment variables have a name and a value associated to it. In this article , let us see how to Create and modify them in various ways.
Solution 1 : Using Command Prompt
Creating and Editing Environment Variables
Both the System and User Environment variables can be edited using this method. However , only the Variable Value can be changed. The Variable name cannot be changed.
Step-1 : Press the Windows+r to open the Run Window. Type cmd and Press OK
Step-2 : Command Prompt Opens. When the following command is entered the variable is created.
For User Environment Variables :
setx <VariableName> "<VariableValue>"
For example , if we want to create a variable TEST with the value C:\TestPath\ we say,
setx TEST "C:\TestPath\"
For System Environment Variables :
setx <VariableName> "<VariableValue>" -m
For example , if we want to create a variable TESTsys with the value C:\ we say,
setx TESTsys "C:\" -m
In order to modify variable’s value, we have to use setx the same command with new value. The setx command re-assigns the Variable value, every time we run it. It stores the latest value in the variable.
In certain cases , the Variable must hold multiple values. In such cases the values should be separated by semicolon.
For User Environment Variable :
setx <VariableName> "<VariableValue1>;<VariableValue2>"
For Example, if we want the variable TEST to hold two values C:\Users\,C:\Test\ we say,
setx TEST "C:\Users\;C\Test\"
For System Environment Variable :
setx <VariableName> "<VariableValue1>;<VariableValue2>" -m
For Example, if we want the variable TESTsys to hold two values C:\Users\,C:\Windows\ we say,
setx TESTsys "C:\Users\;C\Windows\" -m
Step-3 : To verify the changes , Close and open the command prompt and type
echo %VariableName%
To check the value of TEST variable , we say
echo %TEST%
Clearing Environment Variables
When we have to clear the value of a environment variable, the variable value should be empty.
setx <VariableName> ""
Let’s say we have to clear variable named TEST that we created earlier. Then,
setx TEST ""
Deleting Environment Variables
To delete an environment variable enter the following command :
For User Environment Variable :
REG delete "HKCU\Environment" /F /V "<VariableName>"
Lets say, we want to delete TEST variable we created and edited earlier. The command would be
REG delete "HKCU\Environment" /F /V "TEST"
For System Environment Variable :
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V "<VariableName>"
Lets say, we want to delete TESTsys variable we created and edited earlier. The command would be
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V "TESTsys"
Solution 2 : Using Environment Variables Window
Creating and Editing Environment Variables
Step-1 : Open the Run window (Windows+r) and type the following command and press OK
rundll32.exe sysdm.cpl,EditEnvironmentVariables
Step-2 : The Environment Variables window opens
If you want to create a New variable :
- Press New
- New User Variable window Opens
- Give Variable name
- Enter the Variable value
- Press OK
- Press OK again in the Environment Variables Window
If you want to Edit the existing variable :
- Choose the variable to be edited
- Press Edit
- Edit User Variable Window opens
- Edit Variable name
- Edit Variable value
- Press OK
- Again press OK for the changes to be reflected.
NOTE : Using this method
- The System Environment variables cannot be edited.
- Both the Variable Name and value of an User Environment Variable can be edited.
- The Environment variables cannot be cleared.
Deleting Environment Variables
To delete a variable , open the Environment Variables window
- Choose the variable to be deleted
- Click-on Delete
- Click-on OK in the Environment Variables window
Solution 3 : Using PowerShell
Creating and Editing Environment Variables
Both the System and User Environment variables can be edited using this method. However , only the Variable Value can be changed. The Variable Name cannot be changed.
Step-1 : Press Windows+r together to open run window and type powershell
Step-2 : In order to Create the variable type the following command
For User Environment Variable :
[Environment]::SetEnvironmentVariable("<VariableName>","<VariableValue>","User")
Example : To Create avariable name TEST with value C:\TestPath\ we say,
[Environment]::SetEnvironmentVariable("TEST","C:\TestPath\","User")
For System Environment Variable :
[Environment]::SetEnvironmentVariable("<VariableName>","<VariableValue>","Machine")
Example : To Create a variable named TESTsys with value C:\ we say,
[Environment]::SetEnvironmentVariable("TESTsys","C:\","Machine")
In order to edit the variable , type the same command with different variable value. The variable would be updated with the latest value.
When multiple values are to be assigned , the values must be separated with the semi-colon. The commands are as follows
For User Environment Variable :
[Environment]::SetEnvironmentVariable("<VariableName>","<VariableValue1>;<VariableValue2>","User")
Example: Let’s say the variable TEST should contain two values C:\Users\;C\Test\ we say ,
[Environment]::SetEnvironmentVariable("TEST","C:\Users\;C\Test\","User")
For System Environment Variable :
[Environment]::SetEnvironmentVariable("<VariableName>","<VariableValue1>;<VariableValue2>","Machine")
Example: Let’s say the variable TESTsys should contain two values C:\Users\;C\Windows\ we say
[Environment]::SetEnvironmentVariable("TESTsys","C:\Users\;C\Windows\","Machine")
Refer to below example :
To verify the changes , close the powershell window. Open it and type any of the following command :
$Env:<VariableName>
Example : To check the value of variable TEST say,
$Env:TEST
OR
Get-ChildItem Env:\<VariableName>
Example : To check the value of variable TEST say,
Get-ChildItem Env:\TEST
Deleting Environment Variables
To delete a variable, type the following command :
For User Environment Variables :
[Environment]::SetEnvironmentVariable("<VariableName>","","User")
OR
[Environment]::SetEnvironmentVariable("<VariableName>",$null,"User")
Example : To delete the variable TEST say,
[Environment]::SetEnvironmentVariable("TEST","","User")
For System Environment Variables :
[Environment]::SetEnvironmentVariable("<VariableName>","","Machine")
OR
[Environment]::SetEnvironmentVariable("<VariableName>",$null,"Machine")
Example : To delete the variable TESTsys say,
[Environment]::SetEnvironmentVariable("TESTsys",$null,"Machine")
Example Snapshot is shown below
NOTE:
- We cannot Clear the variable using this method.
Thankyou for reading. I hope this helps!