How to download a GitHub Repository using PowerShell

Posted: September 1, 2020  |  Categories: PowerShell

I am automating specific tasks related to my Microsoft Integration, Azure, Office 365, and much more Stencils Pack for Visio project: I started in my last blog post with two simple tasks:

  • Standardize all SVG filenames;
  • List all the detected duplicate files;

And now, I am trying to add more functionalities like: 

  • Automatically install (configure) all the Visio files (*.vssx), so that next time you open Visio, they will be there available;
  • And also the possibility to download the most recent version from GitHub and install it locally;

Well, the first task is quite simple, you just need to locate all the *.vssx files and copy to the folder “C:\Users\you_user\Documents\My Shapes” (that is the default folder for the Visio custom shapes)

#########################################################
#                                                       #
# Install Microsoft Integration & Azure Stencils Pack   #
# Author: Sandro Pereira                                #
#                                                       #
#########################################################

[String]$location = Split-Path -Parent $PSCommandPath
[String]$destination = [environment]::getfolderpath('mydocuments') + "\My Shapes"

$files = Get-ChildItem $location -recurse -force -Filter *.vssx
foreach($file in $files)
{
    if($file.PSPath.Contains("Previous Versions") -eq $false)
    {
        Copy-Item -Path $file.PSPath -Destination $destination -force
    }
}

You can download this script here: Install Microsoft Integration & Azure Stencils Pack.

The second part is a little trick. I found many resources while searching on the internet, but none was what I intended to do. Some were quite nice, but we could easily reach the API Rate limit that is 60 requests per hour for unauthenticated requests or 5000 requests per hour if you use Basic Authentication or OAuth. Or it simply didn’t unzip properly.

I want to archive a simple way to download a full GitHub repository and unzip it locally. Simple as going in the browser and select Code > Download ZIP, that’s it!

And to archive that you can use two ways:

  • using the following URL structure:
    • https://github.com/[owner]/[repo-name]/archive/[Branch].zip
    • This is exactly what Code > Download ZIP does.
  • or using the GitHub API using the following URL structure:
    • https://api.github.com/repos/[owner]/[repo-name]/zipball/Branch]

Since I was doing all this work, I also decide to make it available a generic PowerShell function that will allow you to download any GitHub Repository.

######################################################################
#                                                                    #
# Download and Unzip GitHub Repository                               #
# Author: Sandro Pereira                                             #
#                                                                    #
######################################################################

function DownloadGitHubRepository 
{ 
    param( 
       [Parameter(Mandatory=$True)] 
       [string] $Name, 
        
       [Parameter(Mandatory=$True)] 
       [string] $Author, 
        
       [Parameter(Mandatory=$False)] 
       [string] $Branch = "master", 
        
       [Parameter(Mandatory=$False)] 
       [string] $Location = "c:\temp" 
    ) 
    
    # Force to create a zip file 
    $ZipFile = "$location\$Name.zip" 
    New-Item $ZipFile -ItemType File -Force

    #$RepositoryZipUrl = "https://github.com/sandroasp/Microsoft-Integration-and-Azure-Stencils-Pack-for-Visio/archive/master.zip"
    $RepositoryZipUrl = "https://api.github.com/repos/$Author/$Name/zipball/$Branch"  
    # download the zip 
    Write-Host 'Starting downloading the GitHub Repository'
    Invoke-RestMethod -Uri $RepositoryZipUrl -OutFile $ZipFile
    Write-Host 'Download finished'

    #Extract Zip File
    Write-Host 'Starting unzipping the GitHub Repository locally'
    Expand-Archive -Path $ZipFile -DestinationPath $location -Force
    Write-Host 'Unzip finished'
    
    # remove the zip file
    Remove-Item -Path $ZipFile -Force 
}

Download

THIS POWERSHELL IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND.

Standardize SVG Filenames with PowerShellHow to Download a GitHub Repository with PowerShell
GitHub

#1 Azure Monitoring Platform
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

1 thought on “How to download a GitHub Repository using PowerShell”

  1. hi I tried this code but I am getting empty response.it is saying that The zip file is invalid. The content is not getting downloaded

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top