Permanent Changes to Environment Variables

by Nov 7, 2008

You can easily manage environment variables with the predefined env: drive. For example, to add a new environment variable, type this:

$env:launched = $true

However, these changes are visible only within the current PowerShell session because all changes are applied to the Process set of environment variables. As such, your changes would be visible to all other PowerShell scripts you launched from within the same PowerShell session. Changes are not permanent and will be discarded once you close PowerShell.

To make permanent changes, you will need to access the static .NET methods in the Environment class. This is how you read environment variables:

[Environment]::GetEnvironmentVariable('Temp', 'User')
[Environment]::GetEnvironmentVariable('Temp', 'Machine')

To change values, use SetEnvironmentVariable(). Note that you need full admin privileges to change machine-level environment variables because they affect all users.

[Environment]::SetEnvironmentVariable('Launched', $true, 'User')

Note: Changes you make to user-level and machine-level environment variables are visible in the env: drive only after you restart PowerShell. When PowerShell starts, it receives a copy of all environment variables, and this copy is not updated.