Setting Environment Variables

by Feb 22, 2017

When setting environment variables through the PowerShell “env:” drive, you are always just manipulating the process set. It applies to your current PowerShell instance, and all applications that you start from there. Changes will not persist, though.

To permanently set an environment variable, use this code instead:

$name = 'Test'
$value = 'hello'
$scope = [EnvironmentVariableTarget]::User

[Environment]::SetEnvironmentVariable($name, $value, $scope)

This example sets a new environment variable named “test” with content “hello” on user level. Note that this change will affect only programs that you launch after you set this variable. All new applications receive a “snapshot” of all environment variables in their process set.

To permanently delete an environment variable, set $value to an empty string.

Twitter This Tip! ReTweet this Tip!