Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Last active March 4, 2024 18:39
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save MarkTiedemann/c0adc1701f3f5c215fc2c2d5b1d5efd3 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/c0adc1701f3f5c215fc2c2d5b1d5efd3 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
$tag = (Invoke-WebRequest $releases | 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
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
@mavaddat
Copy link

It would be cool if the script was able to convert the octet-stream ZIP to a binary ZIP file in memory and extract from that without creating any temporary ZIP file on disk.

@f3l3gy
Copy link

f3l3gy commented Nov 10, 2018

https://gist.github.com/f3l3gy/0e89dde158dde024959e36e915abf6bd

Some fixes:

09 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
10 $tag = (Invoke-WebRequest -Uri $releases -UseBasicParsing | ConvertFrom-Json)[0].tag_name

17 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
18 Invoke-WebRequest $download -Out $zip

@Splaxi
Copy link

Splaxi commented May 5, 2020

Did a small refactor on the code:
https://gist.github.com/Splaxi/fe168eaa91eb8fb8d62eba21736dc88a

It supports prerelease (like yours) and also latest release (non-prerelease). It supports inner folder extraction also. It has another logic around handling the extract and destination paths.

@andyneff
Copy link

It was just brought to my attention that this works: https://github.com/$repo/releases/latest/download/$file which is apparently here (although, I'd never find that without knowing the answer first ;)

@MarkTiedemann
Copy link
Author

@andyneff You are right. curl.exe -LO https://github.com/%repo%/releases/latest/download/%file% should be the preferred way for downloading the latest release these days (curl is available by default on Windows and it's faster than PowerShell's Invoke-WebRequest, so I'd recommend using it instead).

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