Updating Environment Variables in the Current Powershell Session


Windows is annoying the way it handles environment variables, especially when you are in the terminal. Changing environment variables outside of the terminal will not cause your terminal session to recognise it. You will need to restart the terminal. This is really annoying when you change variables and need to retry something quick.

You don’t have that problem in Linux, as things are normally better organised, not because env vars are handled different. You still need to source ~/.bashrc to reload them for example.


There is a little cmdlet you can use to instantly reload variables I wrote:

function Update-EnvironmentVariables {
    # can be "Machine", "Process", "User" (see https://docs.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=net-5.0)
    $user = [System.Environment]::GetEnvironmentVariable("Path", "User")
    $machine = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
    $targetPath = $user + ";" + $machine
    $env:Path = $targetPath

I personally have it in the profile and aliased (Set-Alias -Name refreshenv -Value Update-EnvironmentVariables) and it really works.

To explain, Windows stores environment variables in two places - machine-wide and user-wide. User-wide variables take precedence in user’s session, therefore $targetPath takes them first.


Where does Windows actually store environment variables? Think about it… in The Registry!

Machine-wide variables are stored under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment registry key:

and user-wide under HKEY_CURRENT_USER\Environment:

The name for machine-wide is really confusing, but I guess we have to live with it.

You can change env vars from the registry, and they will be applied on next boot for machine-wide ones, and on next log-in for user-wide ones.

If you want to apply them instantly (this does not apply to current terminal session) use setx command or just a windows built-in UI.

Also my favourite environment editor for advanced use cases for many years have been Rapid Environment Editor.

Have a question⁉ Contact me.