Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get Git with PowerShell

Posted on August 25, 2017

If you are creating PowerShell scripts, tools or modules today, you are most likely using Git. What? You're not? Is it because you haven't gotten around to installing it? I have some "quick and dirty" PowerShell hacks to help you out on Windows systems. Linux boys and girls already know what to do.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Naturally you could run a search to find the download link for the latest version of Git. Or if you are using Chocolatey you most likely know how to find and install packages. But perhaps you don't (or can't) use Chocolatey. That's ok. PowerShell can make quick work of this task.

#download the latest 64bit version of Git for Windows
$uri = 'https://git-scm.com/download/win'
#path to store the downloaded file
$path = $env:temp

#get the web page
$page = Invoke-WebRequest -Uri $uri -UseBasicParsing 

#get the download link
$dl = ($page.links | where outerhtml -match 'git-.*-64-bit.exe' | select -first 1 * ).href

#split out the filename
$filename = split-path $dl -leaf

#construct a filepath for the download
$out = Join-Path -Path $path -ChildPath $filename

#download the file
Invoke-WebRequest -uri $dl -OutFile $out -UseBasicParsing

#check it out
Get-item $out

My code assumes you are running on a 64bit system. The comments should explain how it all works. When run you'll get the latest setup executable. I recommend manually installing it so you can configure it as needed.

Related to this you might need a merge conflict tool. I use kdiff3. Here's similar code to download it.

$k = invoke-webrequest https://sourceforge.net/projects/kdiff3/files/kdiff3/0.9.98/KDiff3-64bit-Setup_0.9.98-2.exe/download -UseBasicParsing
$dl = $k.links | where href -match downloads
$dluri = $dl.href.split("?")[0]
$filename = Split-Path $dluri -Leaf
$out = Join-Path $path -ChildPath $filename

Invoke-WebRequest -uri $dluri -OutFile $out -UseBasicParsing

get-item $out

Again, go ahead and install it manually.

My last git hack is to use PowerShell to get the version. If you have git installed, you can easily run

git --version

In Windows you'll get something like git version 2.14.1.windows.1. But being the PowerShell geek that I am, I wanted to display just the version number. So I used a regular expression to extract the value.

$v = git --version
[regex]$rx ="(\d+\.){1,}\d+"
$rx.match($v).value

This same code will also work in open source PowerShell on Linux. Although Windows users can also check the registry:

Get-itemproperty HKLM:\SOFTWARE\GitForWindows | Select CurrentVersion,InstallPath

Want to check the latest git for Windows release to know if you need to download? Use this function.

Function Get-GitCurrentRelease {
[cmdletbinding()]
Param(
[ValidateNotNullorEmpty()]
[string]$Uri = "https://api.github.com/repos/git-for-windows/git/releases/latest"
)

Begin {
    Write-Verbose "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"  

} #begin

Process {
    Write-Verbose "[PROCESS] Getting current release information from $uri"
    $data = Invoke-Restmethod -uri $uri -Method Get

    
    if ($data.tag_name) {
    [pscustomobject]@{
        Name = $data.name
        Version = $data.tag_name
        Released = $($data.published_at -as [datetime])
      }
   } 
} #process

End {
    Write-Verbose "[END    ] Ending: $($MyInvocation.Mycommand)"
} #end

}

image

Fun, right? Now there's no excuse for setting up git on your Windows desktop.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Get Git with PowerShell”

  1. Johnny says:
    September 15, 2017 at 2:54 pm

    The frustrating part is there are those of us that work at financial institutions and you don’t have the access to do anything like instal apps. Not to mention a lot of sites, and URL’s are blocked. heck I have been an some where WSMan is blocked so I had to do all kinds of workarounds.
    If there were flat file alternatives rather than an app you have to install i may be able to get it and run it. Even then sites you connect to to get scripts, etc, often are blocked. It’s like having your hands and feet bound and then thrown into a pool and told to swim.

    1. Jeffery Hicks says:
      September 15, 2017 at 3:00 pm

      I feel your pain. I understand the business case for security and all that it might require, but at some point management has to weigh the cost of security with the cost of getting your job done.

  2. Stéphane B. says:
    October 2, 2017 at 12:25 pm

    Wow I had no idea such a thing as Chocolatey existed in a Windows environment. Thanks Jeff!

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d