In Windows, you sometimes need to modify environment variables.
There are environment variables like
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.
You can edit the environment through the Windows settings You can get to the GUI a few ways:
- Press Windows key and search for "environment" and click
Edit the system environment variablesfrom the Control Panel.
- Press Windows key+R to directly run:
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
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
Check a specific variable like this:
You can print out an environment variable like this:
Set environment variable for a single session
To set an environment variable just for the current command prompt, use
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 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.
setx X ""
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.