Powershell logo

Preface

If you have ever used the PowerShell cmdlet Invoke-WebRequest to download content, you might have observed that it is terribly slow. Files that typically download within seconds with any web browser or tools such as curl or wget, can take minutes to complete with PowerShell.

Solution

The reason that Invoke-WebRequest often is slow because it shows a progress bar when it downloads files. For some odd reason, this causes a major overhead for PowerShell, causing performance degradation.

To disable the progress bar, we only have to add a simple statement in our script before using it. The statement is $ProgressPreference = 'SilentlyContinue'

To provide a complete code example, here we download docker-compose.exe with this fix.

$dockerComposeFolder = "C:\Program Files\Docker"
if (-not (Test-Path $dockerComposeFolder)) { mkdir $dockerComposeFolder }
$url = "https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-windows-x86_64.exe"
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -UseBasicParsing $url -o $Env:ProgramFiles\Docker\docker-compose.exe

This should improve the speed of your scripts considerably when using Invoke-WebRequest.

By Jostein Elvaker Haande

"A free society is a society where it is safe to be unpopular" - Adlai Stevenson

Leave a Reply

Your email address will not be published. Required fields are marked *