Reload the path in PowerShell
Solution 1
Just to bring Rob's comment to light:
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Solution 2
Try getting the machine path and assigning it to the session's path.
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Solution 3
Easiest way, use Chocolatey (freeware). It works for both CMD and PowerShell. Then you will be able to reload PATH (with variable expansion) with a simple command:
refreshenv
Installation from cmd (requires administrator rights):
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
Example usage:
> SET JAVA_HOME=c:/java/jdk6
> SET PATH=%JAVA_HOME%/bin
> ECHO %PATH%
c:/java/jdk6/bin
> SET JAVA_HOME=c:/java/jdk8
> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
> echo %PATH%
c:/java/jdk8/bin
Solution 4
Based on mpen's answer, here is a PowerShell function:
function refresh-path {
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") +
";" +
[System.Environment]::GetEnvironmentVariable("Path","User")
}
Then just call refresh-path
.
Solution 5
Just to add to other answers, you can make sure you don't add superfluous joins by filtering in case the user has an empty path.
$env:Path=(
[System.Environment]::GetEnvironmentVariable("Path","Machine"),
[System.Environment]::GetEnvironmentVariable("Path","User")
) -match '.' -join ';'
Or, more usefully, if you're running a script that adds to a different or multiple environment variables, use a function to reset them all
function resetEnv {
Set-Item `
-Path (('Env:', $args[0]) -join '') `
-Value ((
[System.Environment]::GetEnvironmentVariable($args[0], "Machine"),
[System.Environment]::GetEnvironmentVariable($args[0], "User")
) -match '.' -join ';')
}
resetEnv Path
resetEnv AppPath
Related videos on Youtube

rob
I am a Toronto based software developer currently working with Java and JavaScript
Updated on February 20, 2022Comments
-
rob 8 months
If I have an instance of PowerShell ISE running and I install something that modifies the PATH or I modify it in any way outside of PowerShell then I need to restart PowerShell for it to see the updated PATH variable.
Is there a way to reload the path from within PowerShell without restarting it?
-
rob over 9 yearsThanks that worked! I also had a user environment variable named path so I had to do this: [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
-
kumarharsh over 5 yearsIf you're using chocolatey, and it's loaded into your profile, there's a simpler command:
refreshenv
. This basically runs a more elaborate version of rob's comment. -
Frank Fu about 4 yearsif you are installing chocolatey itself and other apps via chocolatey on the same script which modifies the PATH variable, the
refreshenv
won't work. Therefreshenv
only works on subseqent shells opened. -
Thibault almost 4 yearsThe problem with chocolatery is you can't use it in enterprises, it could help a lot with application automated install and when I search for help I encountered non-native solutions like this...
-
Peter Mortensen almost 4 years
refreshenv
didn't work for me (Windows 10). I still had to open a new window for it to take effect. -
Arkadiusz Przechodzki over 3 yearsI tested and use it exactly on Windows 10, it is useful to me quite often. The usage example I've made is not prepared, it's print from my console. Perhaps in your case it's come kind of conflict between user and system variables? Also, as I've noticed, in multiconsole env (like Conemu) it affects current console only.
-
Reece M about 2 years
refreshenv
also not working here. Working on some scripts in a Windows Sandbox environment and the path just refuses to updated unless a new PS session is started. -
Timo almost 2 years@FrankFu, what if I change path in system Environment Variables in GUI? I think that when installing by choco it ALWAYS changes the path by default. So it is always the same script..
-
Frank Fu almost 2 years@Timo sorry buddy. I'm not sure what you mean. Do you have an example?
-
np8 almost 2 yearsThe path changes are more probably in the
"User"
than in the"Machine"
environmental variables. -
Khale_Kitha over 1 yearAppreciate the command. I ran chocolatey's
refreshenv
command and while it stated that it was refreshing environment variables from registry, it didn't do anything. (It does state that it's refreshing for cmd.exe, and not powershell, so...) - Your command worked. -
DankCoder 7 monthsrefreshenv only works for cmd in my case, doesn't seem to do anything with powershell
-
Matej Kormuth 4 monthsThis should be built-in Powershell command.