4

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.

1
  • 2
    Why are you saving the download as an msi? Commented Aug 11, 2022 at 2:46

6 Answers 6

4
+100

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:

    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

Sign up to request clarification or add additional context in comments.

Comments

2
# 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

Comments

1

This is not the exact answer for your question.

Since you do not prefer something as heavy as WSL, I have a good alternative for your purpose that also promises native windows filesystem support. Use MSYS2 instead of gitbash. This is far better and git-bash is originally based on MSYS2

  1. Download the prefered package of MSYS2.
  2. If you downloaded the GUI installer, install it via CLI with .\msys2-x86_64-latest.exe in --confirm-command --accept-messages --root C:/msys64

Or if you had downloaded the self extracting archive, install it using .\msys2-base-x86_64-latest.sfx.exe -y -oC:\

  1. Lauch MSYS2, then update the packages list with pacman -Syu.

  2. Install git with pacman -S git

You will eventually come to love it. Note that some keyboard shortcuts you are used to in linux may not work example Ctrl+Shift+v for pasting is not supported and Windows uses Shift+Insert

Credits

Comments

1

Just in case, check if the /LOG="C:git-for-windows.log" part of your command has a typo

 /LOG="C:\git-for-windows.log"
        ^^^ 
        (\ was missing)

That way, you can try again, and monitor C:\git-for-windows.log for logs.

Also, make sure you have the right to write directly under C:\.
A /LOG="$env:userprofile\git-for-windows.log" might be safer.

Comments

1

Run the git install once with the SAVEINF parameter, choosing all the options that you'd like to install in the installation UI:

.\Git-2.37.1-64-bit.exe /SAVEINF="c:\temp\git-install.inf"

This will create an install configuration file, which you can use to do a silent install of git using powershell:

$uri = 'https://github.com/git-for-windows/git/releases/download/v2.37.1.windows.1/Git-2.37.1-64-bit.exe'
Invoke-WebRequest -Uri $uri -OutFile git-install.exe
.\git-install.exe /LOADINF="c:\temp\git-install.inf" /VERYSILENT

This will spawn a background process and exit immediately. You can wait for it to complete like this:

while (Get-Process *git-install*) { sleep -seconds 5 }

Comments

1

Now its very easy to use git terminal on PowerShell just use following commands

First, Set execution policy as remotesigned. Run powershell as Administrator and run below command

set-executionpolicy remotesigned

To install the git on powershell

Install-Module posh-git -Scope CurrentUser -Force

To import the git module

Import-Module posh-git

To load profile by default on powershell startup

Add-PoshGitToProfile -AllHosts​​​​​​​

1 Comment

This is the recommened process on Git's site. At a bare minimum it deserved an upvote. Thank you for the contribution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.