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.

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.

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:

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.

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.

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.

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

Cmdlet/Class/Utility Pros Cons
Invoke-WebRequest It is easy to use due to its simple syntax, can handle large files, and is used to retrieve data from websites. It does not serve us with the advanced options to control the download process.
System.Net.WebClient It is a traditional approach used for downloading files and provides us with the basic options to control the download process, for instance, proxy and timeout settings. It is not flexible as the `Invoke-WebRequest` is.
curl It is the easiest and most popular command-line tool to transfer data, which supports different protocols (FTP, HTTP, & more) and is used to perform complex operations, for instance, file uploads. You may have to install this utility on your machine. PowerShell users may need to become more familiar with its syntax.
Start-BitsTransfer It the part of BITS and provides advanced options while downloading files, for instance, pause/resume/restart the download. It is less commonly used than the System.Net.WebClient class and Invoke-WebRequest cmdlet.

Was this post helpful?

Leave a Reply

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