How to download web files with PowerShell? [Quick Guide]

10:38 pm February 20, 201710908

I. introduction

PowerShell language includes the commandlets needed to download files hosted on Internet , be it on an HTTP, HTTPS or FTP hosting, we can download files directly in the command line. It is also possible to specify credentials , if the server that hosts the file requires authentication.

In this tutorial, we won’t talk to download files from an SMB share (or other), but only for files hosted on one web server (which may be on the internet, for example a GitHub file). Finally, this tutorial allows you to answer the following question: how to download files over the internet with PowerShell?

II. download with Invoke-WebRequest

introduced from PowerShell 3.0, the commandlet “ Invoke-WebRequest ” can have many uses because it allows to directly interact with a Web server, whether to make queries GET, POST, etc… It allows to obtain the source code of a page and the parser then.

It also allows, and that’s what interests us today, download files hosted on a web server. For example, an executable hosted on GitHub, I think it’s a good case, because GitHub hosts many files.

Note: Commandlet “Invoke-WebRequest for an alias”wget”which allows to shorten the command and takes the command of the same name under Linux .”

Here are the generic syntax of the Invoke-WebRequest commandlet as part of a download:

 Invoke-WebRequest - Uri "< link-du-file-a-download"-OutFile "" 

the direct link to the EXE file I want to download is: “ https://github.com/tabad/fusioninventory-agent-windows-installer/releases/download/2.3.18/fusioninventory-agent_windows-x64_2.3.18.exe

I want to download the file in the directory “C:temp”. What gives the following command:

 Invoke-WebRequest - Uri "https://github.com/tabad/fusioninventory-agent-windows-installer/releases/download/2.3.18/fusioninventory-agent_windows-x64_2.3.18.exe" - OutFile "C:tempfusioninventory-agent_windows-x64_2.3.18.exe"

Note: it is necessary to specify the name of the .exe output file in the parameter “OutFile”, because it is not implicit. ” You can give the same name as the original one or a different name.

A progress bar indicates the number of bytes downloaded in real time:

with the command “ Get-ChildItem ” followed of the way, we can verify the existence of the file:

if I come back on the functioning of the commandlet “ Invoke-WebRequest “, by default i.e. If one does not put the parameter “OutFile” the result (so the file) is sent in the pipeline which is not what we want when we want to download only one file. On the other hand, if you want to keep the output in the pipeline while downloading the file, it will add the parameter “ – PassThru ” which allows to force the exit pipeline in addition to “ – OutFile “.

To go further: help Invoke-WebRequest

III. Download a file with PowerShell 2.0

now, let’s continue this tutorial with a compatible method PowerShell 2.0, for those who still use this version or who wish to write backward compatible scripts.

With the old method that is when Invoke-WebRequest did not exist, had to go through the creation of an object and then invoke a method to download an object.

We will create an object of type “ System.Net.WebClient ” and store it in a variable “ $WebRequest “. Then, we’ll execute the “DownloadFile” method on this object. this method requires two parameters: the link to the file and the destination path, which should remind you the syntax of Invoke-WebRequest.

 $WebRequest = New-Object System.Net.WebClient $WebRequest.DownloadFile("","") 

if I take the same example as above, here’s the syntax:

 $WebRequest = New-Object System.Net.WebClient $ There is no information on the progress, but the file is actually downloaded. 

Note: You can use the method " DownloadFileAsync " instead of " DownloadFile " to achieve a download in the background, that is, the script does not wait for the download to finish to continue.

To go further: Help Net.WebClient

If you are looking for a solution to download a file through SMB, I direct you to two tracks: