258

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
rob
  • 17,995
  • 12
  • 69
  • 94

6 Answers6

380

Just to bring Rob's comment to light:

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") 
Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 46
    If you're using chocolatey, and it's loaded into your profile, there's a simpler command: `refreshenv`. This basically runs a [more elaborate version](https://github.com/chocolatey/choco/blob/stable/src/chocolatey.resources/helpers/functions/Update-SessionEnvironment.ps1) of rob's comment. – kumarharsh Jan 21 '17 at 20:56
  • 10
    if you are installing chocolatey itself and other apps via chocolatey on the same script which modifies the PATH variable, the `refreshenv` won't work. The `refreshenv` only works on subseqent shells opened. – Frank Fu Aug 02 '18 at 01:47
  • The 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... – Thibault Nov 12 '18 at 16:06
  • @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.. – Timo Nov 20 '20 at 10:34
  • @Timo sorry buddy. I'm not sure what you mean. Do you have an example? – Frank Fu Nov 21 '20 at 09:25
  • Appreciate 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. – Khale_Kitha Apr 19 '21 at 20:22
  • 2
    This should be built-in Powershell command. – Matej Kormuth Jun 12 '22 at 15:57
  • You can create an alias for this in your profile to make it much easier to use. You will need to use a function as described [here](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_aliases?view=powershell-7.3#alternate-names-for-commands-with-parameters). – somethingRandom Feb 27 '23 at 16:17
  • It doesn't work for me. The answer (from Arkadiusz Przechodzki)[https://stackoverflow.com/a/51789626/7856894] works for me. – Fabrício Pereira Jun 08 '23 at 01:47
92

Try getting the machine path and assigning it to the session's path.

$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 62
    Thanks 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") – rob Jul 22 '13 at 22:47
  • The path changes are more probably in the `"User"` than in the `"Machine"` environmental variables. – Niko Föhr Dec 31 '20 at 00:43
56

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 19
    `refreshenv` didn't work for me (Windows 10). I still had to open a new window for it to take effect. – Peter Mortensen Dec 20 '18 at 11:48
  • 1
    I 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. – Arkadiusz Przechodzki Feb 08 '19 at 09:00
  • 1
    `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. – Reece Jul 28 '20 at 15:09
  • 1
    refreshenv only works for cmd in my case, doesn't seem to do anything with powershell – DankCoder Apr 09 '22 at 07:50
  • If you install chocolatey you need `refreshenv` in your PATH. – Johannes Schaub - litb May 19 '23 at 21:36
12

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tjaart
  • 3,912
  • 2
  • 37
  • 61
11

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
Hashbrown
  • 12,091
  • 8
  • 72
  • 95
9

If your path contains environment variables that weren't defined at the start of the session, you'll want to expand those too:

$env:Path = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))

For me this was useful after installing NVM which defines and adds %NVM_HOME% to the path.

To take this to its logical conclusion you could use this recursive function to expand instead:

function Expand-EnvironmentVariablesRecursively($unexpanded) {
    $previous = ''
    $expanded = $unexpanded
    while($previous -ne $expanded) {
        $previous = $expanded
        $expanded = [System.Environment]::ExpandEnvironmentVariables($previous)
    }
    return $expanded
}

And then use:

$env:Path = Expand-EnvironmentVariablesRecursively([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))

I've opened an issue to add this solution into refreshenv from Chocolatey.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Graham
  • 1,529
  • 16
  • 22