How To Create, Edit, Clear and Delete Environment Variables Easily in Windows 10, 11

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:


ADVERTISEMENT

  • 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

Cmd In Run Window

 

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\"

 

User Env Edit

 

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\"

 

Multiple User Ev Edit


ADVERTISEMENT

 

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%

Check Value

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 ""

 

 

Clearing Env

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"

 

Delteing Env

 

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

 

Rundll Command

 

Step-2 : The Environment Variables window opens

If you want to create a New variable :

  1. Press New
  2.  New User Variable window Opens
  3. Give Variable name
  4. Enter the Variable value
  5. Press OK
  6. Press OK again in the Environment Variables Window

 

New Env

 

If you want to Edit the existing variable :

  1. Choose the variable to be edited
  2. Press Edit
  3. Edit User Variable Window opens
  4. Edit Variable name
  5. Edit Variable value
  6.  Press OK
  7. Again press OK for the changes to be reflected.

 

Env Window Changes 1

 

 

NOTE : Using this method


ADVERTISEMENT


  1. The System Environment variables cannot be edited.
  2. Both the Variable Name and value of an User Environment Variable can be edited.
  3. The Environment variables cannot be cleared.

Deleting Environment Variables

To delete a variable , open the Environment Variables window

  1. Choose the variable to be deleted
  2. Click-on Delete
  3. Click-on OK in the Environment Variables window

 

Deleting In 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

 

Powershell In Run

 

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")

 

Creating Var In Ps

 

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 :

 

Mutiple Env Edit In Ps

 


ADVERTISEMENT

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

 

Check In Ps

 

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

 

 

Delete In Ps

 

NOTE:

  • We cannot Clear the variable using this method.

Thankyou for reading. I hope this helps!

ADVERTISEMENT