Tips on how to Use Home windows CMD Atmosphere Variables


Windows 10 and 11 logos

It’s straightforward so as to add or modify an surroundings variable with Command Immediate (CMD), however eradicating one is far more sophisticated. Listed here are a number of alternative ways you are able to do it.

Tips on how to Add or Modify an Atmosphere Variable

First, you might want to launch Command Immediate, or CMD, as an administrator. Click on Begin, sort “cmd” into the search field, after which click on “Run as Administrator.”

Be aware: Any consumer surroundings variable could be set or modified in a daily Command Immediate window, however altering system-wide surroundings variables requires an elevated Command Immediate.

There are two distinct methods to set surroundings variables.

Setting an Atmosphere Variable Quickly

The primary makes use of the set command. Set defines an surroundings variable completely inside the course of during which it has been outlined — in different phrases, the variable solely works within the window you could have open or the script that comprises it.

Right here’s an instance: Let’s say you need to create an surroundings variable named LifeAnswerVar and set the worth to 42. The command could be set LifeAnswerVar=42.

Whereas that window is open, LifeAnswerVar could have the worth 42.

Command Prompt with set lifevar=42

When it’s closed, the surroundings variable and its worth are deleted.

A new CMD window with LifeAnswerVar undefined.

The very same technique works if you wish to quickly modify an current Home windows system variable. All you might want to do is substitute the system variable you need to change instead of LifeAnswerVar, and the worth you need to assign instead of 42.

For instance, in the event you needed to maneuver the TMP folder to C:Instance Folder, you’d enter the command set TMP=C:"Instance Folder".

TMP folde rmoved to Example Folder

The primary line, set TMP, reveals the present worth of TMP. The second line assigns TMP a brand new worth. The third line confirms that it has modified.

Setting an Atmosphere Variable Completely

The second means makes use of setx. Setx defines Home windows surroundings variables completely. They persist between home windows and between restarts, and are written to the Home windows Registry. These surroundings variables could be outlined for a selected consumer, or they are often outlined for system-wide use.

The command setx ExVar1 Tomato /m will create a brand new surroundings variable named ExVar1 and assign the worth “Tomato” to it. The /m argument specifies that the brand new variable needs to be system-wide, not only for the present consumer.

ExVar1 defined in Command Prompt

Use the very same command to switch an current surroundings variable, substituting ExVar1 for the title of the variable you’d like to vary.

Be aware: In case you use setx to switch a variable and set to view the worth of the variable, set is not going to show the suitable worth till a brand new Command Immediate window is opened.

If you wish to add or modify a consumer surroundings variable, simply omit the /m argument from the command.

Tips on how to Take away an Atmosphere Variable

Eradicating an surroundings variable is a bit tougher than including or modifying one.

Be aware: As with including a variable, any consumer surroundings variable could be deleted in a daily Command Immediate window, however deleting a system-wide surroundings variable requires an elevated Command Immediate.

Eradicating an Atmosphere Variable Quickly

If you wish to quickly take away an surroundings variable for the present course of, like a script, PowerShell window, or Command Immediate window, you should utilize the set command. All you might want to do is assign no worth to the variable.

For instance, what when you’ve got the variable definition ExVar1=Tomato within the system-wide surroundings variables, however needed to disregard it for one specific course of? You’ll be able to sort set ExVar1=   into Command Immediate or embrace that line in your script. The variable can be set to nothing whereas the script executes or till you open a brand new Command Immediate window.

ExVar1 temporarily made blank.

Eradicating an Atmosphere Variable Completely

Eradicating an surroundings variable completely is a little more complicated — it’s important to use reg to do it.

Warning: Reg is the command-line model of the Registry Editor. It is best to proceed with warning — a typo may lead to you by chance deleting one thing vital. It by no means hurts to again up the a part of the registry you’re modifying, both.

The surroundings variables for particular person customers are saved in HKEY_CURRENT_USEREnvironment. System-wide surroundings variables are saved elsewhere, in HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment.

Let’s use the ExVar1=Tomato instance. The ExVar1 surroundings variable was outlined system-wide, which implies it’s situated within the HKEY_LOCAL_MACHINE listing relatively than the HKEY_CURRENT_USER listing. Particularly, the trail to the subkey is:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironmentExVar1

Be aware: This path comprises an area. Any time there’s a house in a path entered in a command-line interface, it’s essential to use citation marks across the path, in any other case, it’s exceedingly doubtless that it’ll not execute accurately.

Now we have to use the reg delete command to take away it. Take into account that you’ll must substitute your variable title for ExVar1 within the command under.

reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /f /v ExVar1

There’s a lot there, so let’s break it down a bit.

  • reg delete — defines the applying (reg) and command (delete) we’re utilizing
  • "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" — Tells reg delete the place to search for the important thing
  • /f — Tells reg delete to delete the important thing with out prompting for affirmation
  • /v — Tells reg delete that will probably be given a selected subkey to delete
  • ExVar1 — The title of the subkey we need to delete

Deleting an surroundings variable for a person consumer is strictly the identical as deleting a system-wide variable, besides the trail can be totally different. If ExVar1 have been a consumer surroundings variable, the command to delete it might be:

reg delete HKEY_CURRENT_USEREnvironment /f /v ExVar1

If the command to delete the surroundings variable was profitable, it is best to see “The operation accomplished efficiently” within the Command Immediate.

Reg delete used to remove ExVar1 from user environment variable

Any time you take away an surroundings variable like this, you might want to restart explorer.exe. You’ll be able to restart Explorer.exe manually, or you possibly can simply restart your complete pc. Both will work, and the adjustments ought to take impact instantly after the restart.



Supply hyperlink