Skip to content

Instantly share code, notes, and snippets.

@Splaxi
Forked from MarkTiedemann/download-latest-release.ps1
Last active February 11, 2024 18:13
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Splaxi/fe168eaa91eb8fb8d62eba21736dc88a to your computer and use it in GitHub Desktop.
Save Splaxi/fe168eaa91eb8fb8d62eba21736dc88a to your computer and use it in GitHub Desktop.
Download latest GitHub release via Powershell
# Download latest dotnet/codeformatter release from github
$repo = "jgm/pandoc"
$filenamePattern = "*x86_64.zip"
$pathExtract = "C:\Tools\pandoc"
$innerDirectory = $true
$preRelease = $false
if ($preRelease) {
$releasesUri = "https://api.github.com/repos/$repo/releases"
$downloadUri = ((Invoke-RestMethod -Method GET -Uri $releasesUri)[0].assets | Where-Object name -like $filenamePattern ).browser_download_url
}
else {
$releasesUri = "https://api.github.com/repos/$repo/releases/latest"
$downloadUri = ((Invoke-RestMethod -Method GET -Uri $releasesUri).assets | Where-Object name -like $filenamePattern ).browser_download_url
}
$pathZip = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $(Split-Path -Path $downloadUri -Leaf)
Invoke-WebRequest -Uri $downloadUri -Out $pathZip
Remove-Item -Path $pathExtract -Recurse -Force -ErrorAction SilentlyContinue
if ($innerDirectory) {
$tempExtract = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath $((New-Guid).Guid)
Expand-Archive -Path $pathZip -DestinationPath $tempExtract -Force
Move-Item -Path "$tempExtract\*" -Destination $pathExtract -Force
#Move-Item -Path "$tempExtract\*\*" -Destination $location -Force
Remove-Item -Path $tempExtract -Force -Recurse -ErrorAction SilentlyContinue
}
else {
Expand-Archive -Path $pathZip -DestinationPath $pathExtract -Force
}
Remove-Item $pathZip -Force
@couleurm
Copy link

Hey, I love using your script, thanks for making it! I made my own little reduced version to quickly set up my scripts, thougt I would share it, I made the variables easy to double click and edit

$repo = "REPOURL"
$filenamePattern = "FILTER"
$source= ((Invoke-RestMethod -Method GET -Uri "https://api.github.com/repos/$repo/releases")[0].assets | Where-Object name -like $filenamePattern ).browser_download_url

@Silloky
Copy link

Silloky commented Jul 23, 2022

Thanks, very useful script !

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