How to Download File from URL in PowerShell?

A very common requirement is that you will get to download a file from a URL in PowerShell. In this PowerShell tutorial, we’ll explore different methods to download files from URLs using PowerShell, providing examples and complete scripts for each method.

To download a file from a URL in PowerShell, you can use the Invoke-WebRequest cmdlet with its -Uri parameter for the file’s URL and -OutFile parameter for the destination path. For instance:

Invoke-WebRequest -Uri "https://powershellfaqs.com/wp-content/uploads/2024/02/TestFile.zip" -OutFile "C:\Bijay\TestFile.zip"

This one-liner is a simple and effective way to download files directly from the internet using PowerShell.

1. Using Invoke-WebRequest

The Invoke-WebRequest cmdlet is the most straightforward method to download files in PowerShell. It’s similar to the wget or curl commands in Unix/Linux systems and is part of the standard PowerShell cmdlets.

Here’s an example of how to use Invoke-WebRequest to download a file using PowerShell:

# Define the URL and the destination path
$url = "https://powershellfaqs.com/wp-content/uploads/2024/02/TestFile.zip"
$destination = "C:\Bijay\TestFile.zip"

# Use Invoke-WebRequest to download the file
Invoke-WebRequest -Uri $url -OutFile $destination

This script sets the URL of the file you want to download and the local path where you want to save it, then uses Invoke-WebRequest to download the file from the URL to the specified destination.

You can see the output in the screenshot below after I executed the PowerShell script using Visual Studio code.

Download File from URL in PowerShell

2. Using Start-BitsTransfer

Another method to download files in PowerShell is using the Start-BitsTransfer cmdlet. This cmdlet is part of the BITS (Background Intelligent Transfer Service), which is more robust and suitable for network transfers. It’s especially useful for large files or when you need more control over the transfer process.

Here’s how you can use Start-BitsTransfer to download a file in PowerShell:

# Define the source URL and the destination path
$source = "https://powershellfaqs.com/largefile.iso"
$destination = "C:\Bijay\largefile.iso"

# Use Start-BitsTransfer to download the file
Start-BitsTransfer -Source $source -Destination $destination

This script works similarly to the previous example, but it uses Start-BitsTransfer for the download, which can handle network interruptions and resume downloads.

3. Using New-Object System.Net.WebClient

For those who prefer a more object-oriented approach, you can use the System.Net.WebClient class to download files in PowerShell. This method provides a high level of control over the download process and allows you to manipulate headers, use proxy servers, and more.

Here’s an example using System.Net.WebClient in PowerShell.

# Create a new WebClient object
$webClient = New-Object System.Net.WebClient

# Define the URL and the destination file
$url = "https://powershellfaqs.com/document.pdf"
$destination = "C:\Bijay\document.pdf"

# Download the file
$webClient.DownloadFile($url, $destination)

This script creates a WebClient object, sets the URL and destination, and then downloads the file with the DownloadFile method.

4. Handling Secure Connections (HTTPS)

When you’re downloading files from secure connections (HTTPS), you might encounter issues with SSL/TLS certificates. To handle this, you can adjust the ServicePointManager settings in PowerShell to bypass the certificate check:

# Ignore SSL/TLS certificate errors (use with caution)
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

# Now proceed with the download using any of the methods above
# Create a new WebClient object
$webClient = New-Object System.Net.WebClient

# Define the URL and the destination file
$url = "https://powershellfaqs.com/document.pdf"
$destination = "C:\Bijay\document.pdf"

# Download the file
$webClient.DownloadFile($url, $destination)

Remember that bypassing SSL/TLS checks can expose you to security risks, such as man-in-the-middle attacks. Use this approach only when you’re certain of the source’s safety and integrity.

Conclusion

Downloading files from a URL is a common task that PowerShell makes easy. You can use Invoke-WebRequest, Start-BitsTransfer, or System.Net.WebClient in PowerShell to download a file from a URL in PowerShell.

In this PowerShell tutorial, I have explained how to download file from URL in PowerShell using various methods and examples.

You may also like: