With the following PowerShell function you can download and install git using PowerShell.
It downloads the latest “stable” git release to the given temp directory and installs it with default parameters.
Another and more easy way is using the package manager Chocolatey.
The Script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
function CheckAndInstallGit { Param( [string]$Repo, [string]$TempDir ) try { git } Catch { Write-host "Git not available on your device. " -ForegroundColor Yellow Write-host ": Downloading and installing git..." -ForegroundColor Yellow $InstallGit = $True } If($InstallGit){ $releases = "https://api.github.com/repos/$repo/git/releases" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $Response = (Invoke-WebRequest -Uri $releases -UseBasicParsing | ConvertFrom-Json) $DownloadUrl = $Response.assets | where{$_.name -match "-64-bit.exe" -and $_.name -notmatch "rc"} | sort created_at -Descending | select -First 1 If(!(test-path $TempDir)) { New-Item -ItemType Directory -Force -Path $TempDir | out-null } # --- Download the file to the current location Write-host "Trying to download $($repo) on your Device.." -ForegroundColor Yellow Try{ $OutputPath = "$TempDir\$($DownloadUrl.name)" Invoke-RestMethod -Method Get -Uri $DownloadUrl.browser_download_url -OutFile $OutputPath -ErrorAction Stop } Catch { Write-host $_.exception.message Write-host "Failed to install Git on your laptop, download and install GIT Manually." -ForegroundColor Red Write-host "Download Location: https://gitforwindows.org/" -ForegroundColor Yellow } Write-host "Trying to install GIT on your Device.." -ForegroundColor Yellow Try{ $arguments = "/SILENT" Start-Process $OutputPath $arguments -Wait -ErrorAction Stop } Catch { Write-host $_.exception.message Write-host "Failed to install Git on your laptop, download and install GIT Manually." -ForegroundColor Red Write-host "Download Location: https://gitforwindows.org/" -ForegroundColor Yellow } } Else { Write-host "Git is already installed, no action needed." -ForegroundColor Green } } CheckAndInstallGit -Repo "git-for-windows" -TempDir "c:\temp" |