How to Set Environment Variables Using PowerShell?

Do you want to set the environment variable in PowerShell? In this PowerShell tutorial, I will explain how to set environment variables using PowerShell.

To set environment variables using PowerShell, you can create temporary variables for the session with $env:VARIABLE_NAME = "value", or set them permanently for the system with [System.Environment]::SetEnvironmentVariable('VARIABLE_NAME', 'value', [System.EnvironmentVariableTarget]::Machine). For user-specific variables, replace Machine with User. This allows you to customize your system’s behavior for both the current session and persistently across reboots.

What Are Environment Variables?

Environment variables are key-value pairs stored on a system and used by processes running on that system. One of the most well-known environment variables is PATH, which contains a list of folder locations that the operating system searches for executable files.

Set Environment Variables in PowerShell

To set an environment variable in PowerShell, you can use the Set-Item cmdlet. This cmdlet allows you to modify an existing environment variable or create a new one if it does not already exist.

Temporary Environment Variables

For creating a temporary environment variable that exists only during the current PowerShell session, you can use the $env: drive like so:

$env:MYVARIABLE = "MyValue"

This will set an environment variable named MYVARIABLE with the value MyValue for the duration of your PowerShell session. Once you close the session, the variable will be gone.

Permanent Environment Variables

To set an environment variable permanently, you’ll need to use the [System.Environment] class method SetEnvironmentVariable(). This will set the variable so that it persists across sessions and reboots.

Here’s an example of setting a permanent environment variable:

[System.Environment]::SetEnvironmentVariable('MYVARIABLE', 'MyValue', [System.EnvironmentVariableTarget]::Machine)

In this example, MYVARIABLE is the name of the environment variable, MyValue is the value assigned to it, and [System.EnvironmentVariableTarget]::Machine specifies that the variable should be available system-wide. If you want to set the variable only for the current user, use [System.EnvironmentVariableTarget]::User.

PowerShell to Retrieve Environment Variables

To see the value of your newly created environment variable, you can use the following PowerShell command:


This will output MyValue, the value we set for MYVARIABLE.

Remove Environment Variables using PowerShell

If you need to remove an environment variable in PowerShell, you can use the Remove-Item cmdlet for temporary variables or the SetEnvironmentVariable() method for permanent variables, setting the value to $null.

Here’s how you remove a permanent environment variable:

[System.Environment]::SetEnvironmentVariable('MYVARIABLE', $null, [System.EnvironmentVariableTarget]::Machine)


By using the $env: drive and the [System.Environment] class in PowerShell, you have full control over the environmental variables in your system, which can be extremely useful for scripting and automating tasks. In this PowerShell tutorial, we have learned how to set environment variables using PowerShell.

You may also like: