Reload the path in PowerShell

PowershellConsolePowershell Ise

Powershell Problem Overview


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?

Powershell Solutions


Solution 1 - Powershell

Just to bring Rob's comment to light:

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

Solution 2 - Powershell

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

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

Solution 3 - Powershell

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 - Powershell

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 - Powershell

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

Solution 6 - Powershell

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrobView Question on Stackoverflow
Solution 1 - PowershellmpenView Answer on Stackoverflow
Solution 2 - PowershellShay LevyView Answer on Stackoverflow
Solution 3 - PowershellArkadiusz PrzechodzkiView Answer on Stackoverflow
Solution 4 - PowershellTjaartView Answer on Stackoverflow
Solution 5 - PowershellHashbrownView Answer on Stackoverflow
Solution 6 - PowershellGrahamView Answer on Stackoverflow