It is easy to add or modify an environment variable with Command Prompt (CMD), but removing one is much more complicated. Here are a few different ways you can do it.
How to Add or Modify an Environment Variable
First, you need to launch Command Prompt, or CMD, as an administrator. Click Start, type “cmd” into the search box, and then click “Run as Administrator.”
There are two distinct ways to set environment variables.
Setting an Environment Variable Temporarily
The first uses the set command. Set defines an environment variable exclusively within the process in which it has been defined — in other words, the variable only works in the window you have open or the script that contains it.
Here’s an example: Let’s say you want to create an environment variable named LifeAnswerVar and set the value to 42. The command would be set LifeAnswerVar=42.
While that window is open, LifeAnswerVar will have the value 42.
When it is closed, the environment variable and its value are deleted.
The exact same method works if you want to temporarily modify an existing Windows system variable. All you need to do is substitute the system variable you want to change in place of LifeAnswerVar, and the value you want to assign in place of 42.
As an example, if you wanted to move the TMP folder to C:\Example Folder, you’d enter the command set TMP=C:"Example Folder".
The first line, set TMP, shows the current value of TMP. The second line assigns TMP a new value. The third line confirms that it has changed.
Setting an Environment Variable Permanently
The second way uses setx. Setx defines Windows environment variables permanently. They persist between windows and between restarts, and are written to the Windows Registry. These environment variables can be defined for a specific user, or they can be defined for system-wide use.
The command setx ExVar1 Tomato /m will create a new environment variable named ExVar1 and assign the value “Tomato” to it. The /m argument specifies that the new variable should be system-wide, not just for the current user.
Use the exact same command to modify an existing environment variable, substituting ExVar1 for the name of the variable you’d like to change.
If you want to add or modify a user environment variable, just omit the /m argument from the command.
How to Remove an Environment Variable
Removing an environment variable is a bit harder than adding or modifying one.
Removing an Environment Variable Temporarily
If you want to temporarily remove an environment variable for the current process, like a script, PowerShell window, or Command Prompt window, you can use the set command. All you need to do is assign no value to the variable.
For example, what if you have the variable definition ExVar1=Tomato in the system-wide environment variables, but wanted to ignore it for one particular process? You can type set ExVar1= into Command Prompt or include that line in your script. The variable will be set to nothing while the script executes or until you open a new Command Prompt window.
Removing an Environment Variable Permanently
Removing an environment variable permanently is a bit more complex — you have to use reg to do it.
The environment variables for individual users are stored in HKEY_CURRENT_USER\Environment. System-wide environment variables are stored elsewhere, in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment.
Let’s use the ExVar1=Tomato example. The ExVar1 environment variable was defined system-wide, which means it is located in the HKEY_LOCAL_MACHINE directory rather than the HKEY_CURRENT_USER directory. Specifically, the path to the subkey is:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ExVar1
Now we need to use the reg delete command to remove it. Keep in mind that you’ll need to substitute your variable name for ExVar1 in the command below.
reg delete “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v ExVar1
There is a lot there, so let’s break it down a bit.
reg delete — defines the application (reg) and command (delete) we’re using “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" — Tells reg delete where to look for the key /f — Tells reg delete to delete the key without prompting for confirmation /v — Tells reg delete that it will be given a specific subkey to delete ExVar1 — The name of the subkey we want to delete
Deleting an environment variable for an individual user is exactly the same as deleting a system-wide variable, except the path will be different. If ExVar1 were a user environment variable, the command to delete it would be:
reg delete HKEY_CURRENT_USER\Environment /f /v ExVar1
If the command to delete the environment variable was successful, you should see “The operation completed successfully” in the Command Prompt.
Any time you remove an environment variable like this, you need to restart explorer.exe. You can restart Explorer.exe manually, or you can just restart your entire computer. Either will work, and the changes should take effect immediately after the restart.