Windows env. variables

Photo by Samson on Unsplash

Photo by Samson on Unsplash

How to set up and access environment variables
Updated 10. December 2024

Environment variables are shell variables that can be globally accessed from any program or shell script. In this article, I will show how they are configured on a Windows 10 host, and how to access them using the Command Prompt, PowerShell, and a simple Python script.

Add env. variable

The env. variables for windows are added to the users' list of Environment variables through a GUI in the control panel. This can be accessed easily by opening the start menu and typing the search string "environment"(Fig. 1).

Bilde / Image

Fig. 1 - Search for the env. variable editor

This will open the system properties window. From here click on the button labeled "Environment Variables..." (Fig. 2).

Bilde / Image

Fig. 2 - Click on the Environment variables

The environmental variable list makes it easy to add environmental variables for both your own user and the system. In this example, I will add it to the system variables. The difference between user and system environment variables is that user environment variables are only available for the user it is added to, and system environment variables are available to all users, even service users on the computer.

Click on the "New..." button below the system variables list (Fig. 3).

Bilde / Image

Fig. 3 - Environmental variable list

Then enter a name for the environment variable and a value. (Fig. 4) The name will be used to access the variable, and the value is the value stored in the env. variable.

Bilde / Image

Fig. 4 - Environmental variable editor

The environment variable can now be seen in the list (Fig. 5)

Bilde / Image

Fig. 5 - Environmental variable list

The environment variable is now successfully added and can be accessed from programs through shell interactions.

If you are running a program that is trying to access the environment variable from an IDE like Visual Studio Code, you will have to restart the IDE after the environment variable has been added to make it available. The same applies to shells like BASH, CMD, and PowerShell.

This is because environment variables are only loaded into a program at startup. An IDE shell is a child process of the IDE, and therefore it will inherit environment variables from the IDE.

CMD access

To access the environment variable through the Command Prompt you place a "%" symbol before and after the variable name. A simple example is to use the echo command to print the value to the shell (Fig. 6).

Bilde / Image

Fig. 6 - Printing env. variable in CMD

PowerShell access

To access the environment variable through PowerShell you prefix the variable name with "$env:" when accessing it. This can also be echoed for demonstration purposes.

echo $env:haxorVariable
env variables are great!

Python access

Environment variables are easily accessed from python scripts as well. To access them you will have to import the "getenv" method from the os library. Once imported the getenv method can be used to access the environment variable by passing the environment variable name, and a default value that can be used if the environment variable was not found. In the example below I passed an empty string as a default.

env.py
#!/usr/bin/env python3

from os import getenv

ENV_VAR = getenv("haxorVariable","")
print(ENV_VAR)
python3 env.py  
env variables are great!