Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Why is Using Invoke-WebRequest Much Slower Than a Browser Download?

I use Powershell's Invoke-WebRequest method to download a file from Amazon S3 to my Windows EC2 instance.

If I download the file using Chrome, I am able to download a 200 MB file in 5 seconds. The same download in PowerShell using Invoke-WebRequest takes up to 5 minutes.

Why is using Invoke-WebRequest slower and is there a way to download at full speed in a PowerShell script?

like image 235
Lloyd Banks Avatar asked Feb 23 '15 20:02

Lloyd Banks


People also ask

What is PowerShell invoke-WebRequest?

The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements. This cmdlet was introduced in PowerShell 3.0.

What is the difference between invoke RestMethod and invoke-WebRequest?

Invoke-RestMethod is perfect for quick APIs that have no special response information such as Headers or Status Codes, whereas Invoke-WebRequest gives you full access to the Response object and all the details it provides.


2 Answers

Without switching away from Invoke-WebRequest, turning off the progress bar did it for me. I found the answer from this thread: https://github.com/PowerShell/PowerShell/issues/2138 (jasongin commented on Oct 3, 2016)

$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest <params>

For my 5MB file on localhost, the download time went from 30s to 250ms.

Note that to get the progress bar back in the active shell, you need to call $ProgressPreference = 'Continue'.

like image 122
TinyTheBrontosaurus Avatar answered Oct 12 '22 15:10

TinyTheBrontosaurus


I was using

Invoke-WebRequest $video_url -OutFile $local_video_url

I changed the above to

$wc = New-Object net.webclient
$wc.Downloadfile($video_url, $local_video_url)

This restored the download speed to what I was seeing in my browsers.

like image 58
Lloyd Banks Avatar answered Oct 12 '22 15:10

Lloyd Banks