You have several ways to download a file using PowerShell from the command line. Here are the most common methods:
-
Invoke-WebRequest (Recommended for most cases)
- This is the modern, versatile cmdlet for web requests.
- It shows a progress bar by default in interactive sessions.
Invoke-WebRequest -Uri "https://www.example.com/path/to/your/file.zip" -OutFile "C:\path\to\save\your\file.zip"
Uri: The URL of the file you want to download.
OutFile: The full path (including filename) where you want to save the downloaded file.
Short alias:
You can also use the alias iwr or wget (though wget can be confusing if you also have GNU Wget installed). curl is also an alias for Invoke-WebRequest in modern PowerShell.
iwr -Uri "https://www.example.com/path/to/your/file.zip" -OutFile "C:\downloads\file.zip"
If you encounter SSL/TLS errors (e.g., with self-signed certificates on older systems):
You can bypass SSL validation, but do this with extreme caution and only if you trust the source:
# For older PowerShell versions / specific scenarios
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
# Then run your Invoke-WebRequest command
# OR, specifically for Invoke-WebRequest (PowerShell 6+):
Invoke-WebRequest -Uri "https://self-signed.example.com/file.zip" -OutFile "C:\downloads\file.zip" -SkipCertificateCheck
-
Start-BitsTransfer (Good for large files or background transfers)
- BITS (Background Intelligent Transfer Service) is designed for robust file transfers, especially large ones. It can handle network interruptions and resume downloads.
Start-BitsTransfer -Source "https://www.example.com/path/to/your/largefile.iso" -Destination "C:\path\to\save\your\largefile.iso"
Source: The URL of the file.
Destination: The full path (including filename) where you want to save the file.
By default, Start-BitsTransfer runs asynchronously in the background. If you want your script to wait for the download to complete:
Start-BitsTransfer -Source "..." -Destination "..." | Wait-Job
Or, to make it run synchronously and display progress in the console:
Import-Module BitsTransfer # Ensure module is loaded (usually automatic)
Start-BitsTransfer -Source "..." -Destination "..." -Asynchronous # This seems counterintuitive, but without it, it can sometimes be too backgrounded for immediate console feedback.
# More commonly, you'd do:
$job = Start-BitsTransfer -Source "..." -Destination "..."
while ($job.JobState -eq "Transferring" -or $job.JobState -eq "Connecting") {
Write-Progress -Activity "Downloading" -Status "$($job.JobState)" -PercentComplete ($job.BytesTransferred / $job.BytesTotal * 100)
Start-Sleep -Seconds 1
}
Complete-BitsTransfer -BitsJob $job
However, for a simple command-line download, just running Start-BitsTransfer ... and letting it show its default progress is often sufficient.
-
Using .NET System.Net.WebClient (Older method, still works)
- This involves creating a .NET object and calling its methods.
$sourceUrl = "https://www.example.com/path/to/your/file.txt"
$destinationPath = "C:\path\to\save\your\file.txt"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($sourceUrl, $destinationPath)
Write-Host "File downloaded to $destinationPath"
# Optional: Clean up the object
$webClient.Dispose()
This method doesn't show a progress bar by default.
Remember to replace the URL and the output file path with your actual needs. Ensure the destination directory (C:\Temp\ in the example) exists, or create it first.