Microsoft 365


Active Directory


Windows Server


Chance to win $250 in Petri 2023 Audience Survey



How To Set Environment Variables With PowerShell

Sukesh Mudrakola


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.

How to set an environment variable with PowerShell

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!

What are environment variables in Windows?

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.

The 3 different scopes for environment variables

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.

Machine (system) scope

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.

User scope

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.

Process scope

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.

How to check environment variables with PowerShell

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:

dir env:
Checking environment variables with the dir env: command
Checking environment variables with the dir env: command

Alternatively, you can use the Get-ChildItem cmdlet to check environment variables:

Get-ChildItem -Path Env: 
Using the the Get-ChildItem cmdlet
Checking environment variables with the Get-ChildItem cmdlet

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

How to set environment variables with PowerShell

There are a few different ways to set environment variables using PowerShell. We’ll start with an easy method to do that.

Adding environment variables

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.

Using the $Env variable
Using the $Env variable to set an environment variable

Using the Set-Item cmdlet

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"
The PowerShell Set-Item cmdlet lets you set or create an environment variable.
The PowerShell Set-Item cmdlet lets you set or create an environment variable.

Using the [System.Environment] .NET class method

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.

Using the [System.Environment] .NET class method
Using the [System.Environment] .NET class method to set an environment variable

Are changes to environment variables permanent?

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.

How to remove an environment variable with PowerShell

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:

$Env:MyTestEnvVariable =‘testVariable’

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.

More in PowerShell

Most popular on petri

Article saved!

Access saved content from your profile page. View Saved