Download File from URL in PowerShell

Download File from URL in PowerShell

Using Invoke-WebRequest Cmdlet

Use the Invoke-WebRequest cmdlet to download the specified file from the specified URL to the local path in your machine.

$URL = "https://github.com/PowerShell/PowerShell"
$LocalPath = "E:\Test\README.md"
Invoke-WebRequest -Uri $URL -OutFile $LocalPath

First, we created two variables named $URL and $LocalPath containing a URL and the local path of your machine. Note that variables are prefixed with the $ sign in PowerShell. Next, we used the Invoke-WebRequest cmdlet to download a file from the URL. Here, the file can be an image, text, html, markdown or any other file.

We used the Invoke-WebRequest cmdlet to get the content from the specified web page on the internet. This cmdlet sends the HTTP, HTTPS, FILE and FTP requests to a web service or web page. Then, it parses the response from the web page and returns a collection of links, images, files, forms and other significant HTML elements. To use this cmdlet, you must have PowerShell version 3.0 or above.

The -URI parameter was used to specify the Uniform Resource Identifier (URI) of an internet resource to which we have sent the web request. The HTTP, HTTPS, FILE, and FTP values are supported by this parameter. This parameter’s value is required to use the Inovke-WebRequest cmdlet, while we can omit the parameter name, -URI. See the following example.

$URL = "https://github.com/PowerShell/PowerShell"
$LocalPath = "E:\Test\README.md"
Invoke-WebRequest $URL -OutFile $LocalPath

Next, we used the -OutFile cmdlet followed by the $LocalPath. This cmdlet was used to specify the output file; we can mention the path and the file name with the extension here. The -OutFile cmdlet saved the response body for the given output file. The output file will be saved in the current directory if we don’t specify the path.

We also have an alias for the Invoke-WebRequest cmdlet, which is iwr that we can use as follows:

$URL = "https://github.com/PowerShell/PowerShell"
$LocalPath = "E:\Test\README.md"
iwr -Uri $URL -OutFile $LocalPath

Using System.Net.WebClient Class

Use the System.Net.WebClient class to download a file from the specified URL to the local path in your machine.

$URL = "https://github.com/PowerShell/PowerShell"
$LocalPath = "E:\Test\README.md"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($URL, $LocalPath)

After creating and initializing the $URL and $LocalPath variables, we used the New-Object cmdlet to create an object of the WebClient class and saved its reference in the $webClient variable. The System.Net.WebClient provides us with common methods that we can use to send or receive data from the specified resource identified by Uniform Resource Identifier (URI).

The WebClient class uses the WebRequest class to provide access to the given resources. Note that the WebClient objects (also called WebClient instances) can retrieve data with any WebRequest descendant registered with the WebRequest.RegisterPrefix method. By default, the URIs starting http:, ftp:, https:, and file: scheme identifiers are supported by the .NET Framework.

Finally, we used the .DownloadFile() method of the WebClient class to download a file from the specified URL to a local path. This method took two parameters, an address and a filename, which are stored in the $URL and $LocalPath variables. This method generates different exceptions based on various things:

  • The .DownloadFile() method produces an ArgumentNullException exception if the filename parameter or address (URI) parameter is null. The ArgumentNullException will be thrown when a null reference is passed to the method for which it is not a valid argument.

  • This method will generate NotSupportedException if it is called on multiple threads simultaneously.

  • We will get WebException for different reasons:

    • If the address is invalid or the Uniform Resource Identifier is formed by combining the BaseAddress.

    OR

    • The given filename is empty, null, or does not exist

    OR

    • We got an error while downloading data.

It is not recommended to use the WebClient for new development. Instead, we can use the System.Net.Http.HttpClient class.

Using curl Utility

Use the curl utility to download a file from the specified URL to the local path in your machine.

$URL = "https://github.com/PowerShell/PowerShell"
$LocalPath = "E:\Test\README.md"
& curl $URL -OutFile $LocalPath

In the previous section, we learned about the $URL, $LocalPath and -OutFile. Here, we used the curl utility to download a file from the web page. The & operator was used to run the curl command in the current shell.

If the curl is not installed on your machine, you must install it first and then use it to download a file.

Using Start-BitsTransfer Cmdlet

Use the Start-BitsTransfer cmdlet to download a file from the specified URL to the local path in your machine.

$URL = "https://download.microsoft.com/download/8/8/0/880BCA75-79DD-466A-927D-1ABF1F5454B0/PBIDesktopSetup.exe"
$LocalPath = "E:\Test\PBIDesktopSetup.exe"
$startTime = Get-Date
Import-Module BitsTransfer
Start-BitsTransfer -Source $URL -Destination $LocalPath

In the above code, we imported the BitsTransfer module and used the Start-BitsTransfer cmdlet to transfer the specified file from the source to the destination. We also recorded the start time using the Get-Date cmdlet and stored it in the $startTime variable, which we can use to compare the current time with the $startTime to determine how long this transfer took to be completed.

The Start-BitsTransfer cmdlet is used to create a BITS (Background Intelligent Transfer Service) transfer job to transfer one or multiple files between a server and a client machine. This cmdlet helps us to download multiple files from the specified server to a given client computer, but it does not let us upload multiple files from a client computer to the server. If we are required to upload multiple files, we can use Import-Csv to pipe the output to the Add-BitsFile cmdlet or consider a compressed file (.zip) or a cabinet file (.cab).

BITS is the operating system’s component which enables reliable, scalable, and efficient transfer of large files over the network. We often use this to transfer files in the background, which let us pause, resume and restart the transfer, even if the transfer was interrupted or your system was restarted. We specify the source and destination of the file using the -Source and -Destination parameters while using the Start-BitsTransfer cmdlet. We can also use various options for a transfer, for instance, transfer type and priority, etc.

Now, we have learned various ways to download a file from a webpage with PowerShell, so it is mandatory to know the pros and cons of each of them.

Comparing All of the Above Solutions

[wpdatatable id=18]

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.