How to refresh the environment of a PowerShell session after a Chocolatey install without needing to open a new session How to refresh the environment of a PowerShell session after a Chocolatey install without needing to open a new session powershell powershell

How to refresh the environment of a PowerShell session after a Chocolatey install without needing to open a new session


You have a bootstrapping problem:

  • refreshenv (an alias for Update-SessionEnvironment) is generally the right command to use to update the current session with environment-variable changes after a choco install ... command.

  • However, immediately after installing Chocolatey itself, refreshenv / Update-SessionEnvironment themselves are only available in future PowerShell sessions, because loading these commands happens via code added to profile $PROFILE, based on environment variable $env:ChocolateyInstall.

That said, you should be able to emulate what Chocolatey does when $PROFILE is sourced in future sessions in order to be able to use refreshenv / Update-SessionEnvironment right away, immediately after installing Chocolatey:

iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))choco install -y git# Make `refreshenv` available right away, by defining the $env:ChocolateyInstall# variable and importing the Chocolatey profile module.# Note: Using `. $PROFILE` instead *may* work, but isn't guaranteed to.$env:ChocolateyInstall = Convert-Path "$((Get-Command choco).Path)\..\.."   Import-Module "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"# refreshenv is now an alias for Update-SessionEnvironment# (rather than invoking refreshenv.cmd, the *batch file* for use with cmd.exe)# This should make git.exe accessible via the refreshed $env:PATH, so that it# can be called by name only.refreshenv# Verify that git can be called.git --version

Note: The original solution used . $PROFILE instead of Import-Module ... to load the Chocolatey profile, relying on Chocolatey to have updated $PROFILE already at that point. However, ferventcoder points out that this updating of $PROFILE doesn't always happen, so cannot be relied upon.


You can try and use Update-SessionEnvironment:

Updates the environment variables of the current powershell session with any environment variable changes that may have occured during a Chocolatey package install.

That will test if that change is still effective after the chocolatey call.

If not, one easy workaround would be at least to use an absolute path for calling git.

To call Git from Powershell:

new-item -path alias:git -value 'C:\Program Files\Git\bin\git.exe'

Then you can try:

git clone --mirror https://${username}:${password}@$hostname/${username}/$Projectname.git D:\GitTemp -q 2>&1 | %{ "$_" } 


I go with the lo-tech solution:

$env:Path += ";C:\Program Files\Git\bin"