Get the download url of the latest GitHub release using PowerShell
![](https://blog.jmorbegoso.com/img/automation-scripting.webp)
When we want to download the latest version of a GitHub release, we often do it manually from GitHub.com, but there are times when we need this to be an automatic process, either because we must do it on many computers or should it be done without any user interaction. For these scenarios, we can make use of the following PowerShell script:
function Get-Last-GitHub-Release-Url {
[CmdletBinding()]
param(
[Parameter(Mandatory = $TRUE)]
[string]
$GitHubRepository,
[Parameter(Mandatory = $TRUE)]
[string]
$FilenamePattern
)
$releasesUri = "https://api.github.com/repos/$GitHubRepository/releases/latest";
$response = Invoke-RestMethod -Method "GET" -Uri $releasesUri;
$assets = $response.assets;
$lastRelease = $assets | Where-Object "name" -Match "$FilenamePattern";
return $lastRelease.browser_download_url;
}
This function receives 2 parameters:
GitHubRepository
This is the repository identifier, we can get it from the repository webpage, for example:
In the repository:
The identifier is:
gohugoio/hugo
FilenamePattern
This is the filename pattern of the file that we want to download, for example:
Open the releases section of the repository https://github.com/gohugoio/hugo/releases/latest:
We will see a list of all the files of that release, something like this:
Assets │── hugo_0.105.0_linux-amd64.tar.gz │── hugo_0.105.0_openbsd-amd64.tar.gz │── hugo_0.105.0_windows-amd64.zip │── hugo_0.105.0_windows-arm64.zip │── hugo_extended_0.105.0_linux-amd64.tar.gz │── hugo_extended_0.105.0_openbsd-amd64.tar.gz │── hugo_extended_0.105.0_windows-amd64.zip │── hugo_extended_0.105.0_windows-arm64.zip │
So, if I want to get the url of the release for Windows x64:
hugo_extended_0.105.0_windows-amd64.zip
Just replace the version of the file with a “.” (dot/period) metacharacter:
hugo_extended_(.+)_windows-amd64.zip
And that’s all, now we can use the PowerShell function to get the download url of the release of that GitHub repository:
Get-Last-GitHub-Release-Url -GitHubRepository "gohugoio/hugo" -FilenamePattern "hugo_extended_(.+)_windows-amd64.zip";