How to Use Windows CMD Environment Variables


    Windows 10 and 11 logos

    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.”

    Click "Run as Administrator."

    Note: Any user environment variable can be set or modified in a regular Command Prompt window, but changing system-wide environment variables requires an elevated Command Prompt.

    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.

    Command Prompt with set lifevar=42

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

    A new CMD window with LifeAnswerVar undefined.

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

    TMP folde rmoved to 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.

    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 to modify a variable and set to view the value of the variable, set will not display the right value until a new Command Prompt window is opened.

    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.

    Note: As with adding a variable, any user environment variable can be deleted in a regular Command Prompt window, but deleting a system-wide environment variable requires an elevated Command Prompt.

    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.

    ExVar1 temporarily made blank.

    Removing an Environment Variable Permanently

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

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

    The environment variables for individual users are stored in HKEY_CURRENT_USEREnvironment. System-wide environment variables are stored elsewhere, in HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironment.

    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_MACHINESYSTEMCurrentControlSetControlSession ManagerEnvironmentExVar1

    Note: This path contains a space. Any time there is a space in a path entered in a command-line interface, you must use quotation marks around the path, otherwise, it is exceedingly likely that it will not execute correctly.

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

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

    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.





    Source link

    Previous articleStudy of Apple’s ATT impact highlights competition concerns – TechCrunch
    Next articleSonic Dash, Gear.Club Stradale Arrive on Apple Arcade