Skip to content

Instantly share code, notes, and snippets.

@f3l3gy
Forked from MarkTiedemann/download-latest-release.ps1
Last active November 8, 2023 07:12
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save f3l3gy/0e89dde158dde024959e36e915abf6bd to your computer and use it in GitHub Desktop.
Save f3l3gy/0e89dde158dde024959e36e915abf6bd to your computer and use it in GitHub Desktop.
Download latest GitHub release via Powershell
# Download latest dotnet/codeformatter release from github
$repo = "dotnet/codeformatter"
$file = "CodeFormatter.zip"
$releases = "https://api.github.com/repos/$repo/releases"
Write-Host Determining latest release
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$tag = (Invoke-WebRequest -Uri $releases -UseBasicParsing | ConvertFrom-Json)[0].tag_name
$download = "https://github.com/$repo/releases/download/$tag/$file"
$name = $file.Split(".")[0]
$zip = "$name-$tag.zip"
$dir = "$name-$tag"
Write-Host Dowloading latest release
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest $download -Out $zip
Write-Host Extracting release files
Expand-Archive $zip -Force
# Cleaning up target dir
Remove-Item $name -Recurse -Force -ErrorAction SilentlyContinue
# Moving from temp dir to target dir
Move-Item $dir\$name -Destination $name -Force
# Removing temp files
Remove-Item $zip -Force
Remove-Item $dir -Recurse -Force
@JoshDesmond
Copy link

JoshDesmond commented Mar 15, 2020

Just a note for anyone looking at this for inspiration: I've found that another repository, posh-ssh, uses the "latest release" tag on github, which means you could potentially simplify part of this script using the url: https://github.com/darkoperator/Posh-SSH/releases/latest, for example. dotnet/codeformatter doesn't appear to do so, however.

Either way, this is a pretty neat script, thanks!

Edit: Here's an example:

$repo = "darkoperator/Posh-SSH"

$release = "https://github.com/$repo/releases/latest"
$tag = (Invoke-WebRequest -Uri $release -UseBasicParsing)
$download = ($tag.Links -match "/$repo/releases/download/").href
$download = "https://github.com$download"
Invoke-WebRequest "$download" -OutFile "c:/whereyouwant/filename.zip"
Expand-Archive "c:/whereyouwant/filename.zip"  -DestinationPath "c:/whereveryouwant/filename"
Remove-Item "c:/filename.zip" -Force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment