Pegavo
Pegavo

Reputation: 15

PowerShell download exe from public Github

I'm unable to download a .exe file from Github. Script works for different sites and downloads files without issues.

https://github.com/ShareX/ShareX/releases/tag/v13.1.0

This is the .exe I'm trying to download: https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe

>$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
>$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Invoke-WebRequest : The remote server returned an error: (403) Forbidden.
At line:2 char:16
+ $WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Full script:

[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Write-Output "Downloading $DownloadUrl"
Start-BitsTransfer -Source $WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace("%20", " ") -Destination "C:\Users\Pegavo\Desktop\PS\"

Upvotes: 1

Views: 3220

Answers (2)

Lam Le
Lam Le

Reputation: 1839

You can just simply use Invoke-WebRequest with -OutFile parameter.

Invoke-WebRequest https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe -OutFile "ShareX-13.1.0-setup.exe"

This command will download the file from GitHub and store the result to a file in your current directory.

Or, you can use WebClient.

$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe", "E:\your\path\ShareX-13.1.0-setup.exe")

I tested both on my local machine. Both worked.

Moreover, if you want to understand why Start-BitsTransfer won't work, here.


Edit:

You can get the filename automatically like this one, using Split-Path:

$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file

Upvotes: 1

Eden Koveshi
Eden Koveshi

Reputation: 61

Is there a reason for using HEAD? GET seems to work

Upvotes: 0

Related Questions