Want some programming help? [close]

Set Environment Variables in Windows

Introduction

In Windows, you sometimes need to modify environment variables. There are environment variables like %APPDATA% and %PROGRAMFILES% which contain useful paths, and others that contain things like your username (%USERNAME%). An important one that you may want to modify is %PATH%. In this guide we will look at how to set, check, update, and unset environment variables using the GUI and the command prompt.

Note that environment variables are case-insentive and that there are system-wide environment variables and user-specific environment variables.

Using GUI

You can edit the environment through the Windows settings You can get to the GUI a few ways:

  1. Press Windows key and search for "environment" and click Edit the system environment variables from the Control Panel.
  2. Press Windows key+R to directly run: SystemPropertiesAdvanced.exe

Once in the System Properties panel, click the "Environment Variables..." button in the bottom-right. From the GUI, you can edit the user variables on the top and the system variables on the bottom.

After setting an environment variable using this GUI method, you will need to restart your applications for it to take effect.

Using command prompt

Use set and setx to manipulate environment variables from the command line. Get help with set /? and setx /?. Let's look at what each one does.

Check an environment variable

Check all environment variables with set:

set

Check a specific variable like this:

set X

You can print out an environment variable like this:

echo %X%

Set environment variable for a single session

To set an environment variable just for the current command prompt, use set.

set X=something

Permanently set environment variable

To permanently set an environment variable that will persist across command prompts and through restart, use setx. Get help with setx /?.

setx X 123

By default, this is a user environment variable, not a system one. To set a system environment variable, add the /M flag (will require admin prompt).

A common scenario is the need to update your PATH. In this situation you want to add to the existing variable. You can do that by referencing the original value like this:

setx PATH "%PATH%;C:\opt\bin\"

Unset environment variable

To unset an environment variable, set to to an empty string.

set X=

or

setx X ""

Conclusion

After reading this, you should know how to set environment variables in Windows permanently or for a single session using the GUI or the command prompt.

Advertisement