Set Environment Variables Using PowerShell

Marion Paul Kenneth Mendoza Jan 30, 2023 Apr 30, 2022
  1. What Are Environment Variables
  2. Set an Environment Variable With Env: in PowerShell
  3. Set the [System.Environment] .NET Class in PowerShell
  4. Refresh Environment Variables on Current PowerShell Session
Set Environment Variables Using PowerShell

Using Windows PowerShell to set Windows environment variables, read environment variables and create new environment variables is easy once we know the proper execution of commands.

PowerShell provides many different methods to interact with Windows environment variables from the $env: PSDrive and the [System.Environment] .NET class. This article will discuss setting environment variables and refreshing them in our current session using PowerShell.

What Are Environment Variables

As the name suggests, environment variables store information about Windows and applications’ environments.

We can access environment variables through a graphical interface such as Windows Explorer and plain text editors like Notepad, cmd.exe, and PowerShell.

Using environment variables helps us avoid hard-coding file paths, user or computer names, and more in our PowerShell scripts or modules.

Set an Environment Variable With Env: in PowerShell

We can create new environment variables with PowerShell using the New-Item cmdlet. But, first, provide the environment variable’s name in the Env:\<EnvVarName> format for the Value parameter, as shown below.

Example Code:

New-Item -Path Env:\TEST -Value WIN10-DESKTOP


Name                           Value
----                           -----
TEST                           WIN10-DESKTOP

We can use the Set-Item cmdlet to set an environment variable or create a new one if it doesn’t already exist. For example, we can see below using the Set-Item cmdlet.

We can both make or modify an environment variable.

Example Code:

Set-Item -Path Env:TEST -Value "TestValue"

Set the [System.Environment] .NET Class in PowerShell

The [System.Environment] will use a few different .NET static class methods. We don’t need to understand what a static way is.

We only need to understand to use any of the techniques we are about to learn, and we will need first to reference the class ([System.Environment]) followed by two colons (::) then followed by the method.

To set the environment variable using the .NET class stated, use the SetEnvironmentVariable() function to set the value of an existing environment variable for the given scope or create a new environment variable if it does not already exist.

When setting variables in the process scope, we will find that the process scope is volatile and existing in the current session while changes to the user and machine scopes are permanent.

Example Code:


Refresh Environment Variables on Current PowerShell Session

To use our new set of environment variables in our PowerShell session, get the environment variable of the user profile and machine through the .NET class and assign it to the PowerShell environment variable.

Since environment variables are also considered PowerShell variables, we can assign values to them directly more straightforwardly.

Example Code:

$env:PATH = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.


Related Article - PowerShell Environment Variables