PowerShell: download a file in HTTP, HTTPS

In this tutorial, I will explain how to download a file using PowerShell that is on a server accessible in HTTP or HTTPS.

This can be particularly useful when administering a server without an interface, where the only browser installed is Internet Explorer or in a script.

There are several solutions to download one using PowerShell that we will see in this tutorial.

Download a file with Invoke-WebRequest

We will start with the Invoke-WebRequest Cmdlet which is available since PowerShell 3.0

To use the Invoke-WebRequest cmdlet, we need to pass it at least two parameters:

-Uri : this parameter will contain the URL that we want to call, it is not necessary to name the parameter if it is placed first.
-OutFile : the location and name of the file.

To illustrate this tutorial, I will download the Zabbix Agent at the following url:https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.3/zabbix_agent2-6.4.3-windows-amd64-openssl.msi

Invoke-WebRequest https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.3/zabbix_agent2-6.4.3-windows-amd64-openssl.msi -Outfile C:\Download\zabbix-agent2.msi

The Invoke-WebRequest Cmdlet can also be called using an alias well known to Linux users, which is wget.

You have to pass the same parameters, just replace Invoke-WebRequest with wget:

wget https://cdn.zabbix.com/zabbix/binaries/stable/6.4/6.4.3/zabbix_agent2-6.4.3-windows-amd64-openssl.msi -Outfile C:\Download\zabbix-agent2.msi

This method should work on most Windows servers and computers.

Download a file with Start-BitsTransfer

The Start-BitsTransfer cmdlet allows you to download a file using (BITS) Background Intelligent Transfer Service which is a component integrated into Windows with the following benefits:

  • Multiple download in background task
  • Resumption in case of stoppage

Start-BitTransfer needs two parameters:

-Source : Url of the file to download, the parameter can be omitted on the url is indicated in the first position
-Destination : Location where the file will be saved.

Unlike Invoke-WebRequest, you just have to indicate the location without the file name.

Here’s how to use Start-BitsTransfer:

Start-BitsTransfer https://releases.ubuntu.com/22.04.2/ubuntu-22.04.2-live-server-amd64.iso -Destination C:\Download

Download file with New-Object with PowerShell 2.0

If you’re on an “old” server that doesn’t have PowerShell 3.0, it’s still possible to upload a file with PowerShell using New-Object to create a System.Net.WebClient object available in .NET Framework.

There are several syntaxes to use this method, here is one to download the GLPI agent available at this url: https://github.com/glpi-project/glpi-agent/releases/download/1.4/GLPI-Agent-1.4 -x64.msi

(New-Object System.Net.WebClient).DownloadFile("https://github.com/glpi-project/glpi-agent/releases/download/1.4/GLPI-Agent-1.4-x64.msi","C:\Download\GLPIAgent.msi")

As you can see, the second argument has the path and filename.

Leave a Comment