1

I'm having troubles to find a way to get git and poshgit working after installing it through a script using chocolatey without closing the powershell console.

Here is what my script currently looks like.

Function InstallChocolatey {
    iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    refreshenv  #needed to make poshgit install succesfully without restarting Powershell
    Write-Output "Chocolatey installed and configured."
}

Function InstallSoftware {
    choco install git --params='/NoShellIntegration' -y
    choco install poshgit -y
    . $profile   #reload profile
    Write-Output "Software installed and profile reloaded"
}

InstallChocolatey
InstallSoftware

When I close Powershell and restart it everything works as expected. But as my script should later continue executing git stuff I would really like to find a solution to make it work without closing the Console.

From what I found on Stackoverflow and other sites using

. $profile

should reload the Profile. But unfortunately I my case it doesn't seem to have any effect. I tried to use refreshenv again.

My profile file currently only contains one line

 Import-Module 'C:\tools\poshgit\dahlbyk-posh-git-a4faccd\src\posh-git.psd1'

I also tried adding -force at the end of the line but nothing changed.

I'm pretty new to Powershell, so please bear with me... :)

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
Pascal Mages
  • 177
  • 1
  • 11
  • Required restart is required. – Ansgar Wiechers Apr 11 '17 at 20:45
  • @AnsgarWiechers so what you say there is no way around a manual restart? – Pascal Mages Apr 11 '17 at 20:49
  • its not required, for me `. $profile` works. And I'm talking specifically about choco – 4c74356b41 Apr 11 '17 at 20:52
  • You could try `start powershell.exe; exit`. I doubt you'll get poshgit to work without starting a new PowerShell instance. – Ansgar Wiechers Apr 11 '17 at 20:53
  • I think the restarting part allows to reload environment variables, which might be useful for `poshgit`? – sodawillow Apr 11 '17 at 21:06
  • @4c74356b41 can you confirm it works for you also with poshgit? – Pascal Mages Apr 11 '17 at 21:07
  • @sodawillow I actually tried it with refreshenv as well which is supposed to reload the environment variables but that didn't change a thing. – Pascal Mages Apr 11 '17 at 21:10
  • 1
    for posh git you can do this: `. 'C:\Program Files\WindowsPowerShell\Modules\posh-git\0.6.1.20160330\profile.example.ps1'` or something like that, just load that file into memory – 4c74356b41 Apr 11 '17 at 21:16
  • @4c74356b41 thanks for the hint. I found the file at `C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1` Loading it gives me these `WARNUNG: git not in path WARNUNG: Could not find ssh-agent WARNUNG: posh-git's profile.example.ps1 will be removed in a future version. WARNUNG: Consider using Add-PoshGitToProfile -StartSshAgent instead.` – Pascal Mages Apr 20 '17 at 09:47

2 Answers2

0

Thanks to the comments above I was able to get it to work without restarting PowerShell (see script below)!

The key elements needed to make it work are:

Refresh the environment variables after installing Chocolatey and Git/Posh Git

refreshenv 

In Order to make Git work without PS restart temporarily add Git to path

$env:path+='C:\Program Files\Git\cmd' 

and last but not least reload Posh Git Profile (as suggested by @4c74356b41)

. C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1

The complete script (improvements are welcome!):

    Function InstallChocolatey {
    iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
    #choco feature enable -n autoUninstaller
    refreshenv
    Write-Output "Chocolatey installed."
}


Function InstallSoftware {
    choco install git --params='/NoShellIntegration' -y
    $env:path+='C:\Program Files\Git\cmd'
    Write-Output "Git installed"
    choco install poshgit -y
    refreshenv
    Write-Output "Posh Git installed"
}

Function WriteSSHKeys { #Writing SSH keys to the user directory
}

Function SetupSSH {
    . C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1
}

InstallChocolatey
InstallSoftware
WriteSSHKeys
SetupSSH
Pascal Mages
  • 177
  • 1
  • 11
  • Using `. C:\tools\poshgit\dahlbyk-posh-git-a4faccd\profile.example.ps1` to reload the Posh Git Profile actually throws a warning and suggests to use `Add-PoshGitToProfile -StartSshAgent` instead. Unfortunately I was unable to get it to work using the way Posh Git suggests it. – Pascal Mages Apr 20 '17 at 14:28
0

Only this worked for me.

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

Source: https://shellgeek.com/powershell-refresh-environment-variables/