Download files and repositories from GitHub with PowerShell

Image Description

Daily PowerShell #34

Daily PowerShell GitHub DevOps

November 19, 2021

In this post, we’ll look at how to download files from GitHub with PowerShell without having to use a git client.

Download a File From GitHub

You can use Invoke-WebRequest to download files from GitHub. In this example, we’ll look at the plinqo repository.

To download a single file, you can access the raw file data. To locate the URL for a file, click the Raw button on GitHub.

The linked URL will link to the githubusercontent.com domain. To save the file, you can use a similar command line to below. -OutFile is the location of the local file to write.

Invoke-WebRequest 'https://raw.githubusercontent.com/ironmansoftware/plinqo/main/README.md' -OutFile ./README.md

Download a Repository From GitHub

You can also clone a repository without using a git client. On the main page of the repository, click the Code button and then right click on the Download ZIP option and select Copy Link.

The link that is copied will point directly to the ZIP file for the branch you have selected. You can use a combination of Invoke-WebRequest and Expand-Archive to clone to the current directory.

Invoke-WebRequest 'https://github.com/ironmansoftware/plinqo/archive/refs/heads/main.zip' -OutFile .\plinqo.zip
Expand-Archive .\plinqo.zip .\
Rename-Item .\plinqo-main .\plinqo
Remove-Item .\plinqo.zip

Download a GitHub Gist

GitHub gists are small snippets of code. They let you quickly create tiny repositories that can contain one or more files but don’t have issues, actions or wikis.

Similar to GitHub files, you can download gists by getting the raw URL of the file to download. There is a Raw button on each file within a gist. I’m using the Windows 11 Taskbar at Top registry file for this example.

To download the gist, use the same syntax as downloading a file from a repository.

Invoke-WebRequest 'https://gist.githubusercontent.com/adamdriscoll/849f31e9360b3b994d7ee75dbd3b44ff/raw/205c6250c13a899d6184334143b0794a55c43dfa/bottom.reg' -OutFile .\bottom.reg