One of my team members recently searched for a PowerShell script to download files from a URL. I suggested different methods to achieve this. In this tutorial, I will explain how to download a file from a URL using PowerShell.
Download a File from a URL Using PowerShell
There are multiple ways to download files using PowerShell. I will cover three primary methods:
- Using the Invoke-WebRequest cmdlet (built-in to PowerShell)
- Using the System.Net.WebClient class (.NET class)
- Using the Start-BitsTransfer cmdlet (requires BITS service)
Let me show you each method with examples.
1. Using Invoke-WebRequest
The Invoke-WebRequest
cmdlet is the best way to download files from a URL using PowerShell. It allows you to send HTTP/HTTPS requests and save the response content to a file.
Here’s an example of downloading a ZIP file of US historical weather data:
$url = "https://www.spguides.com/wp-content/uploads/2024/12/WeatherInfo.zip"
$output = "C:\WeatherData\WeatherInfo.zip"
Invoke-WebRequest -Uri $url -OutFile $output
This downloads the file from the specified URL and saves it to the output path on your local machine.
In this example:
$sourceUrl
contains the URL of the file to be downloaded.$destinationPath
specifies the local path where the file will be saved.Invoke-WebRequest
fetches the file and saves it to the specified path.
I executed the above PowerShell script, and you can see it displays the exact output in the screenshot below; it downloaded the zip file and saved it on the local drive.
Sometimes, you might receive an error like The remote server returned an error: (403) Forbidden.
If the server requires authentication, you can include credentials:
$sourceUrl = "https://www.spguides.com/wp-content/uploads/2024/12/WeatherInfo.zip"
$destinationPath = "C:\WeatherData\WeatherInfo.zip"
$username = "your-username"
$password = "your-password"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $securePassword)
try {
Invoke-WebRequest -Uri $sourceUrl -OutFile $destinationPath -Credential $credentials -Headers @{
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
Write-Output "Download successful!"
} catch {
Write-Output "An error occurred: $_"
}
Check out Check If File Modified In Last 24 Hours Using PowerShell
2. using System.Net.WebClient
Let me show you another method to download a file from a URL using PowerShell.
The System.Net.WebClient class provides another way to download files in PowerShell. It’s a .NET class so it requires creating a WebClient object first.
Example of downloading US national park data:
$url = "https://www.nps.gov/subjects/developer/api-documentation.htm#/parks/getPark"
$output = "C:\WeatherData\us-parks.json"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
This creates a new WebClient object, then uses its DownloadFile() method to save the file from the URL to the specified local path.
Here is the exact output in the screenshot below:
If you want to handle timeout as well as any errors, then you can use the try catch block. Let me show you another example.
$sourceUrl = "https://example.com/financial-report.pdf"
$destinationPath = "C:\Downloads\financial-report.pdf"
$webClient = New-Object System.Net.WebClient
$webClient.Timeout = 60000 # Timeout in milliseconds
try {
$webClient.DownloadFile($sourceUrl, $destinationPath)
Write-Output "Download successful!"
} catch {
Write-Output "An error occurred: $_"
}
Read How to Create a Folder If Not Exists in PowerShell?
3. using Start-BitsTransfer
Start-BitsTransfer is a PowerShell cmdlet for the Background Intelligent Transfer Service (BITS). It supports resuming interrupted downloads, download throttling, and asynchronous transfers.
Example downloading US Census data:
$url = "https://www2.census.gov/programs-surveys/popest/datasets/2020-2021/cities/totals/SUB-IP-EST2021-ANNRNK.csv"
$output = "C:\MyFolder\us-city-population-2021.csv"
Start-BitsTransfer -Source $url -Destination $output
This uses BITS to download the CSV file of 2021 US city population estimates from the Census Bureau website to the specified local destination.
Some key advantages of Start-BitsTransfer are:
- Ability to pause and resume downloads
- Configurable transfer rates to throttle bandwidth usage
- Asynchronous transfer mode for parallel downloads
However, the BITS service must be running on your system.
Read How to Create a File in PowerShell if it Doesn’t Exist?
Invoke-WebRequest Vs. System.Net.WebClient Vs. Start-BitsTransfer
Here’s a comparison table to help you decide which method to use:
Method | Ease of Use | Supports Authentication | Handles Large Files | Automatic Resume |
---|---|---|---|---|
Invoke-WebRequest | Easy | Yes | No | No |
Start-BitsTransfer | Moderate | No | Yes | Yes |
System.Net.WebClient | Moderate | Yes | No | No |
Conclusion
In this tutorial, we covered three different ways to download a file from a URL using PowerShell:
- Invoke-WebRequest
- System.Net.WebClient
- Start-BitsTransfer
I have also provided examples for each method. I hope you found this tutorial helpful for understanding how to use PowerShell to download files from the web. Let me know if you have any other questions!
You may also like:
- PowerShell Get-date Add Days Examples
- How to Create a Log File using PowerShell?
- How to Compare Dates in PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com