I love moving electrons
01 Mar, 2015
Windows Environment Variable Stuff

Let’s try setting up environment variables on Windows OS (I am on Windows 8 box) from a command-line. Before we start some pointers:

  1. There are two types of environment variable we are concerned about here - User and System. User environment variables are valid for the current user session/profile. System environment variables are valid system wide irrespective of user session.

  2. If same variable name exists in both User and System, the User variable takes precedence. However, for Path variable the value from User is appended to System.

  3. Once the environment variables are created/modified from command line, they do not take effect in the current command prompt session. You have to start a new cmd session.

Listing Environment Variables

Fire up cmd.exe in administrator mode. The first command we are going to use is SET. To list the environment variables that have been setup run:

D:\>set

This will list all effective environment variables for your user session. To list all variables that start with 'P':

D:\>set P

There is another way to list the environment variables - using REG command. This will list user variables:

D:\>REG QUERY HKEY_CURRENT_USER\Environment

And this will list system variables:

D:\>REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"


Setting Environment Variables

To set a new user variable we need to use the SETX command:

D:\>setx myvar1 "Hello Mr. Robot"

To set a new system variable run:

D:\>setx myvar2 "Hello Mr.Robot" /m

To append values in existing variables:

D:\>setx Path "%Path%;C:/maven/bin;"

Appending the Path variable is a bit different because %Path% returns the combined values of both User and System Path variables, which means the new Path User variable will be combination of [old System Path variable + old User Path variable + new path values].

Deleting Environment Variables

To clear an environment variable, use the following command, this will not delete the variable, just clears it:

D:\>setx myvar1 ""
D:\>setx myvar2 "" /m

Unfortunately, there is no cousin command to SETX that can delete environment variables. This has to be done by removing from registry using REG command. Practice caution here - you are about to change registry entries so be careful! Run this command to delete environment variable:

D:\>REG DELETE "HKEY_CURRENT_USER\Environment" /v myvar1
D:\>REG DELETE "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v myvar2

comments powered by Disqus