Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save r35krag0th/079275297fb7a41c15eec93a3c83f0d0 to your computer and use it in GitHub Desktop.
Save r35krag0th/079275297fb7a41c15eec93a3c83f0d0 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment