Environment Variables Explained: How to Access, Create, and Delete them on Windows

by Mayank

Operating Systems have a number of terms that the average computer user may not have heard of. Enivronment variables is one of those terms. You may have heard this term from a colleague at your workplace or a friend. It is also very likely that some program on your machine is giving errors related to environment variables.

Environment Variables exist for all operating systems, such as Linux , MacOS or Windows. By principle, they mean the same thing with all OS, but the way they are implemented and used is distinct. In this article, I will discuss how environment variables are implemented in Windows.

Contents

  1. What are Environment Variables?
  2. Types of Environment Variables
  3. Common Environment Variables
  4. Accessing your current Environment Variables
  5. Creating Environment Variables
  6. Deleting Environment Variables
  7. Bonus: Doing some cool stuff

What are Environment Variables?

Environment variables, as the name implies, are variables that describe the environment in which programs run. When you run a program or application on your computer, the application needs answers to questions like-What is the name of the computer where it’s installed? What’s the name of the user account where the program is running? Where is Windows installed on your computer? Where are temporary files stored on your computer? Where is a particular folder located on your computer?

All these questions are answered by the Environment Variables.

In Windows, all environment variables have a Name and a Value. For instance, the variable %windir% has a value which is equal to the path in which Windows is installed on your computer. Here windir is the variable name and the path to your Windows installation directory is the value of the variable.

Environment variables are placed between mod signs to distinguish them from regular text like %path%, %temp%, %homepath% etc. I will explain what these terms mean in a bit. But first it is important that you know the types of environment variables.

Types of Environment Variables

Environment variables are of 2 types- User and System variables.

User environment variables are user-specific or, in other words, they have values that differ from user to user. They are user-specific and store user-specific data such as the location of your user profile or the folder where temporary files created by various programs are stored for your user account. They can only be edited by that user account and not by other accounts. The user, Windows or various programs working with user specific locations can set the names and values of user environment variables.

System variables are global and can not be changed by the user even if you can access them. Their values are the same for all computer user accounts. They usually hold the locations of critical system resources, such as the folder where Windows is installed. The %windir% variable I mentioned above is a system variable.They also store the location of the Program Files folder on your computer. These variables can be set by windows, by different programs and drivers.

Common Environment Variables

  • %path% – The path environment variable specifies the location where a program’s executable files are located on the computer. The program may be anything- Photoshop, Microsoft Paint, etc. The path variable also contains the location of several Windows directories such as System32, Program Files, etc.

Have you ever wondered, When you open a program let ‘s say Google Chrome from your desktop, how does the computer know where the executable file (chrome.exe) is located?

This location is identified by Windows through the path variable. The google chrome location is stored as a value to the path variable. So, when you launch the Chrome browser from your desktop, Windows asks for the location where Google Chrome is installed to the path variable and executes chrome.exe from that location.

  • %programfiles% – This variable holds the location of the Program Files folder on your computer as it’s value (Usually the value is C:\Program Files).
  • %ComputerName% – This variable stores the name of your computer, which is JulianAssange in my case.
  • %temp% – Another common environment variable is temp, which contains some temporary data from different programs. For example, when you open MS Word, a process is created by Windows. This process needs to store some temporary resources for MS Word to run. These resources will be stored in your machine’s temp folder. The location of this temp folder can be accessed from the %temp% environment variable.

There are more environment variables available on Windows. You can learn more about them here.

Accessing your Current Environment Variables

You can easily find out which environment variables are currently being used on your computer. You can access them either by using the Windows Desktop or by using the command prompt (CMD).

Using Windows Desktop

  • Go to MyPC>Properties>Advanced System Settings.
Advanced system settings
  • Select Environment Variables.
Environment Variables
  • You can also do the following: Search for Environment Variables>Edit System Environment Variables/Edit Environment Variables for your account>Environment Variables.
Search window

Using Command Prompt

  • Press Windows+R to open the Run window.
  • Type cmd in the dialog box to launch the Windows command prompt.
Starting the command prompt
  • Type SET or set (The command isn’t case sensitive) and press Enter. You will get the list of all the environment variables (User and System both) on your computer.
Listing all the variables
  • If you want to access the contents of just one variable then type echo followed by the name of the variable (For example echo %path% to access the Path variable).
Displaying the values of the path variable

Creating Environment Variables

If you’ve read this article so far, you should have a basic idea of what environment variables actually mean. Now you’re supposed to know how to create them. You can basically create environment variables directly from your Windows Desktop, or you can also use the command prompt (CMD).

Using Windows Desktop

  • Go to Computer>Properties>Advanced System Settings>Environment Variables.
Environment Variables
  • You can either create a user or a system environment variable. Create a system variable if you have multiple user accounts or might create one in the future and want this new variable to be globally accessible across all user accounts on your computer.
  • Click New and a dialog box will open asking for the name and value of the new variable.
Creating a new variable
  • For example, I want to create a path variable for the Photoshop program on my PC. To do this, I’ll set the variable name as Path.
  • For the variable value, I will navigate to the location where Photoshop is installed and copy the address of that location.
Location of the Photoshop executable on my PC
  • Then paste the address in the Value field and click OK.
Creating a new Path variable
  • We’ve successfully created an environment variable, and now my computer knows where Photoshop is installed.
The Path variable is created
  • You can follow the same steps to create a Path variable for any program you want.

NOTE: For most programs on your PC, you need not worry about creating the Path variable, as the program installer does that automatically for you. However, if you are a programmer and use the prompt command to compile and execute your code, you may need to set the path variable so that the command window can locate the executables from the particular programming language directory.

Learn how to set environment variables for Python.

Using Command Prompt

  • Press Windows+R to open Run and type cmd in the dialog box to open the Windows command prompt.
Starting the command prompt
  • Type setx followed by the variable name and the value. The format goes like:
setx “VariableName VariableValue”
  • For example, I’ll set a path variable containing the location of the Photoshop executable file on my PC:
setx Path “C:\Program Files\Adobe\Adobe Photoshop CC 2018”
  • Hit Enter to execute the command. The variable is now created.
Creating a new Path variable
  • You can check to see if the variable is actually created by typing echo %path%.

Deleting Environment Variables

Now that you know how to create environment variables, deleting them is very easy and quick. Again we can either use the Windows Desktop or the command prompt to delete them.

Using Windows Desktop

  • Go to Computer>Properties>Advanced System Settings>Environment Variables.
Environment Variables
  • Find the variable you want to delete in either the User column or the System column.
  • Click on the variable and then click on the Delete button next to New.
Deleting an environment variable
  • The variable should now be successfully deleted.

Using Command Prompt

  • Run Command Prompt with administrator privileges.
Running command prompt as administrator
  • Type the following command and hit Enter:
setx VariableName " "
  • The above command will delete the value of the variable but not the variable itself. To remove the variable completely, you need to remove it from the registry editor. To do that, execute the command:
REG delete HKCU\Environment /F /V VariableName
  • The variable should now be successfully deleted.

Bonus: Doing Some Cool Stuff with Environment Variables

Now that you know what environment variables are, and you can also create and delete them, it’s time for me to show you some wonderful things you can do with them.

You can create custom environment variables for the browser to open a website. In the example below, I created a custom environment variable to open Google:

Creating a variable named Google

Now, if I type %Google% in the Run window and hit Enter, the link www.google.co.in will open in your default browser. Pretty Cool, huh?

Hitting OK will open Google search inside your default browser

We love to hear from you

Are you still confused? Got some questions? Feel free to comment below. Also, feel free to suggest more topics for future blog posts.

Related Posts

Leave a Comment