Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install git (with bash on PowerShell) on PowerShell?

https://gitforwindows.org/ has an option to put bash into PowerShell. I need that so no installing WSL and etc. I need to install git unattended, that is, with command line only. Existing tutorials like this only launch the installer using PowerShell, but I have to use the mouse to install stuff.

So, how can I install git, with bash on PowerShell, using PowerShell?

UPDATE:

I tried

Write-Host "Installing Git for windows..." -ForegroundColor Cyan
$exePath = "$env:TEMP\git.msi"

Write-Host "Downloading..."
(New-Object Net.WebClient).DownloadFile('https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe', $exePath)

Write-Host "Installing..."
Start-Process msiexec.exe -Wait -ArgumentList '$exePath /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh" /LOG="C:git-for-windows.log"'

git --version
bash

but it gets stuck on "Installing..." and does not print any other outputs.

like image 405
Guerlando OCs Avatar asked Sep 20 '25 16:09

Guerlando OCs


2 Answers

There are two problems:

  1. Git for Windows does not get released as an MSI package. And you cannot convert a regular executable into an MSI package just by renaming it. You do not need msiexec.exe at all. The installer itself has already paramaters to perform a silent installation. Just execute it as is:

    $exePath = "$env:TEMP\git.exe"
    Start-Process $exePath -Wait -ArgumentList '/NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh" /LOG="C:\git-for-windows.log"'
    

    But: This will sill launch a GUI. So you have to add more parameters to make the installation really silent. Further reading:

    • Git: Silent or Unattended Installation
    • Git For Windows Silent Install Silent Arguments

    TL;DR: Also add /VERYSILENT and you might want to use /LOADINF to customize some settings.

  2. After the successful installation, you will face the same problem, you already did in your similar question, I just answered. TL;DR:

    The environment variables in your current Process scope are not updated automatically. Update them manually by:

    foreach($level in "Machine","User") {
       [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
          # For Path variables, append the new values, if they're not already in there
          if($_.Name -match 'Path$') { 
             $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
          }
          $_
       } | Set-Content -Path { "Env:$($_.Name)" }
    }
    

    This code is taken from this answer.

    After that, git --version and Get-Command git will work.


Full script:

$exePath = "$env:TEMP\git.exe"

# Download git installer
Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe -UseBasicParsing -OutFile $exePath

# Execute git installer
Start-Process $exePath -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' -Wait

# Optional: For bash.exe, add 'C:\Program Files\Git\bin' to PATH
[Environment]::SetEnvironmentVariable('Path', "$([Environment]::GetEnvironmentVariable('Path', 'Machine'));C:\Program Files\Git\bin", 'Machine')

# Make new environment variables available in the current PowerShell session:
foreach($level in "Machine","User") {
   [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
      # For Path variables, append the new values, if they're not already in there
      if($_.Name -match 'Path$') { 
         $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
      }
      $_
   } | Set-Content -Path { "Env:$($_.Name)" }
}

# Work with git
git --version
bash

like image 100
stackprotector Avatar answered Sep 22 '25 06:09

stackprotector


# Make new environment variables available in the current PowerShell session:
function reload {
   foreach($level in "Machine","User") {
      [Environment]::GetEnvironmentVariables($level).GetEnumerator() | % {
         # For Path variables, append the new values, if they're not already in there
         if($_.Name -match 'Path$') { 
            $_.Value = ($((Get-Content "Env:$($_.Name)") + ";$($_.Value)") -split ';' | Select -unique) -join ';'
         }
         $_
      } | Set-Content -Path { "Env:$($_.Name)" }
   }
}
Write-Host "Installing git..." -ForegroundColor Cyan

$exePath = "$env:TEMP\git.exe"

Invoke-WebRequest -Uri https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe -UseBasicParsing -OutFile $exePath

Start-Process $exePath -ArgumentList '/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS="icons,ext\reg\shellhere,assoc,assoc_sh"' -Wait

[Environment]::SetEnvironmentVariable('Path', "$([Environment]::GetEnvironmentVariable('Path', 'Machine'));C:\Program Files\Git\bin", 'Machine')

reload

git --version
bash --version
like image 24
Guerlando OCs Avatar answered Sep 22 '25 06:09

Guerlando OCs