Now Reading
How to use Windows CMD Environment Variables
[vc_row thb_full_width=”true” thb_row_padding=”true” thb_column_padding=”true” css=”.vc_custom_1608290870297{background-color: #ffffff !important;}”][vc_column][vc_row_inner][vc_column_inner][vc_empty_space height=”20px”][thb_postcarousel style=”style3″ navigation=”true” infinite=”” source=”size:6|post_type:post”][vc_empty_space height=”20px”][/vc_column_inner][/vc_row_inner][/vc_column][/vc_row]

How to use Windows CMD Environment Variables

Windows 10 and 11 logos

Windows 10 and 11 logos

It’s easy to add or modify environment variables with Command Prompt. However, it can be more difficult to remove them. Here are some ways you can do this.

How to add or modify an environment variable

First, you must be able toLaunch Command Prompt (or CMD) as an administrator. Click Start, type “cmd” into the search box, and then click “Run as Administrator.”

Click

Note:You can set and modify any user environment variable in a regular Command Prompt window. However, you will need to use an elevated Command Prompt to change system-wide variables.

There are two ways to set environment variables.

Setting an Environment Temporarily Variable

The set command is used for the first. 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.

LifeAnswerVar’s value 42 will be displayed while the window is open.

Command Prompt with set lifevar=42

When the variable is closed, its value and the environment variable are deleted.

A new CMD window with LifeAnswerVar undefined.

You can also temporarily modify an existing Windows system parameter using the exact same procedure. You just need to substitute LifeAnswerVar for the system variable that you wish to modify, and the value that you wish to assign for 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".

TMP folde rmoved to Example Folder

The first line Set TMPThe current value of TMP is shown in the second line. The second line gives TMP a new value. The third line confirms that TMP has changed.

Permanently setting an Environment Variable

Setx is used in the second method. Setx defines Windows environment variables permanently. They are saved to the Windows environment variables file and can be used between windows or restarts. Windows Registry. These environment variables can either be set up for a particular user or for the entire system.

The command setx ExVar1 Tomato /m will create a new environment variable named ExVar1 and assign the value “Tomato” to it. The /m argument tells the system that the new variable must be accessible to all users and not just the current user.

ExVar1 defined in Command Prompt

Use the exact same command to modify an existing environment variable, substituting ExVar1 for the name of the variable you’d like to change.

Note:If you use setx for modifying a variable and set to see the value, set won’t display the correct value until a new Command Prompt window opens.

To add or modify an environment variable in the user’s user interface, you can omit the argument /m from the command.

How to Remove an Environment Variable

It is more difficult to remove an environment variable than it is to add or modify one.

Note:Like adding a variable to the environment, you can delete any user environment variable in a regular Command Prompt window. However, deleting a system-wide environment variables requires an elevated Command Prompt.

Removal of an Environment Variable Temporarily

The set command can be used to temporarily remove an environmental variable from a process like a script or PowerShell window. All you have to do is to assign no value to the variable.

Consider, for example, what happens if you have the variable defined ExVar1=TomatoYou want to ignore the system-wide environment variables for a particular process but not in the system-wide environment variables? You can type ExVar1 = Input the following line into Command Prompt. Until you open a new Command Prompt, the variable will be set null while the script executes.

ExVar1 temporarily made blank.

Permanently removing an environment variable

Removing an environment variable permanently is a bit more complex — you have to use Reg to do it.

Warning:Reg is the command-line interface to the Registry Editor. You should proceed with caution — a typo could result in you accidentally deleting something important. It’s never a bad idea to back up the part of the registry you’re editing, either.

The environment variables for each user are stored in HKEY_CURRENT_USEREnvironment. System-wide environmental variables are stored elsewhere, in HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment.

Let’s use the ExVar1=Tomato example. ExVar1 is a system-wide environment variable that was created. It is located in the HKEY_LOCAL_MACHINE directory and not the HKEY_CURRENT_USER directories. The path to the Subkey is:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironmentExVar1

Note:This path contains a single space. If there is a space in the path you enter in a command line interface, you must put quotation marks around it. Otherwise it is very likely that it won’t execute correctly.

Now we need the reg deleteTo remove it, use the following command. Keep in mind that you’ll need to substitute your variable name for ExVar1 in the command below.

reg delete "HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" /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_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment" — 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

It is exactly the same thing as deleting a system-wide environment variable for an individual user, except that the path will differ. If ExVar1 were an environment variable for a user, the command to delete it is:

reg delete HKEY_CURRENT_USEREnvironment /f /v ExVar1

If the command to delete the environment variable was successful, you should see “The operation completed successfully” in the Command Prompt.

Reg delete used to remove ExVar1 from user environment variable

You must restart Explorer.exe after you have removed an environment variable such as this. Either manually restart Explorer.exe or your entire computer. Either option will work and the changes should take place immediately after restarting.

View Comments (0)

Leave a Reply

Your email address will not be published.