Environment variables allow you to access command line tools and control the execution of programs on your operating systems. In this article, we’ll explain how environment variables work and how to set an environment variable with PowerShell.
PowerShell provides an easy way to access, edit, and clear the Windows environment variables. PowerShell can also be used to set an environment path, edit existing paths, and handle the user profile working on the computer.
To set a new environment variable with PowerShell, you can use the Set-Item cmdlet. This lets you modify an environment variable or create a new one if it doesn’t already exist.
Here’s the syntax you need to use:
Set-Item -Path Env:<variable-name> -Value ($Env:<variable-name> + ";<new-value>")
Keep reading to learn different ways to set an environment variable in Powershell!
Environment variables contain crucial information needed for the execution of your operating system and applications. They include information about directory paths and the location of certain core files for the operating system to function.
Environment variables also collect information about your system’s execution, and they can store data between reboots and sessions. On Windows, there are multiple ways to access and manage environment variables, and that includes using Windows File Explorer, text editors like Notepad, command prompt, and PowerShell.
PowerShell allows you to manage and access environment variables in all supported operating systems. It lets you access, change, clear, and even delete them when needed.
On Windows, there are three different scopes for environment variables. These scopes follow a Machine > User > Process hierarchy, and each of them is capable of overwriting the parent one if needed.
The machine or system scope contains all environment variables that are related to the system and are associated with Windows instances. System variables can be seen and accessed by any user accessing the system. However, you need to have sufficient privileges to be able to change these variables.
The user scope contains the environment variables linked to the user currently running processes on Windows. They can take priority over the machine or system variables, and with sufficient privileges, they can overwrite the system scope variables that have the same name.
The process scope contains all the environment variables associated with the process or PowerShell session that’s currently running. Environment variables under the process scope are a combination of both Machine and User scopes, and they’re usually inherited from the parent process along with a few Windows-created dynamic variables.
All the environment variables in PowerShell are stored in a PS drive called ‘Env:‘. There are multiple ways you can retrieve the environment variables along with their values in your operating system using PowerShell.
First, you can use the dir env: command:
Alternatively, you can use the Get-ChildItem cmdlet to check environment variables:
Get-ChildItem -Path Env:
You can also retrieve a specific environment variable by appending the variable name after the aforementioned commands.
Example: To retrieve HomeDrive (an example variable), you can use the command below:
Get-ChildItem -Path Env:\HomeDrive
There are a few different ways to set environment variables using PowerShell. We’ll start with an easy method to do that.
To add an environment variable with PowerShell, you can use the $Env variable to either set an environment variable using the assignment operator (=), or create a new one using the (+=) operator.
For example, if you wish to create the AZURE_RESOURCE_GROUP environment variable in your system in case it doesn’t already exist by default, you can do so using the command below:
$env:AZURE_RESOURCE_GROUP = ‘SampleResourceGroup'
Note: If the AZURE_RESOURCE_GROUP already existed in your system, it will be replaced with the value you pass in the command above.
The Set-Item cmdlet can be used to change and retrieve the value of environment variables. You can also use the Set-Item cmdlet to set or create an environment variable.
The cmdlet below will set the environment variable AZURE_RESOURCE_GROUP to ‘SampleResourceGroup2.’
Set-Item -Path env:AZURE_RESOURCE_GROUP -Value “SampleResourceGroup2"
Microsoft’s .NET framework class library is a powerful way to use and execute PowerShell scripts. The .NET class [System.Environment] offers methods to set environment variables.
To proceed, you need to use the SetEnvironmentVariable() method for a given scope. You can also create a new one if it doesn’t already exist.
For example, you can use SetEnvironmentVariable() to set the AZURE_RESOURCE_GROUP environment variable using the command below.
Due to the nature of the different scopes for environment variables, your mileage may vary. All changes made to an environment variable in the process scope are volatile as they only apply to the current process (session). However, changes made to environment variables in the user or machine scopes are permanent to that respective user and machine.
There are different ways to remove or delete an environment variable with PowerShell. Firstly, you can remove them using the $Env or SetEnvironmentVariable() method of the .NET class.
For this example, let’s create a test environment variable called ‘MyTestEnvVariable’ using the following cmdlet:
Now, to remove this system-wide environment variable, you can use the cmdlet below that uses the SetEnvironmentVariable() method:
Alternatively, you can also use the $Env variable to clear and delete the environment variable using the cmdlet below:
$Env:MyTestEnvVariable = $null
In this article, we’ve detailed how environment variables work on Windows, as well as different ways to set them up with PowerShell. However, it’s important to understand the 3 different scopes for environment variables, and you also need to keep in mind that some methods to set them only apply to the current PowerShell session.