Illustrative Examples: Download File From Url Powershell

PowerShell’s download capabilities extend far beyond basic URL fetches. This section dives into practical examples, demonstrating how to handle diverse download scenarios efficiently and robustly. From simple file grabs to complex automated workflows, these examples showcase the power and flexibility of PowerShell.
PowerShell offers a dynamic approach to downloading files, making it adaptable to various situations. The examples provided illustrate techniques for handling different file types, sizes, and security requirements. These demonstrations are designed to empower you to craft sophisticated download scripts tailored to your specific needs.
Downloading a File from a Specific URL
This example demonstrates downloading a file from a given URL using `Invoke-WebRequest`. It showcases the basic structure and error handling.
“`powershell
$url = “https://www.example.com/myfile.txt”
$destinationFile = “C:\downloads\myfile.txt”
try
Invoke-WebRequest -Uri $url -OutFile $destinationFile
Write-Host “File downloaded successfully to $destinationFile”
catch
Write-Error “Error downloading file: $_”
“`
This script fetches a file from the specified URL and saves it to the designated location. A `try…catch` block is crucial for handling potential errors, such as network issues or invalid URLs.
Downloading and Extracting a Compressed Archive
This example shows how to download a compressed archive (like a .zip file) and extract its contents. It uses `Expand-Archive` for efficient decompression.
“`powershell
$url = “https://www.example.com/archive.zip”
$destinationFolder = “C:\downloads\extracted_files”
try
# Download the archive
Invoke-WebRequest -Uri $url -OutFile “$destinationFolder\archive.zip”
# Extract the archive
Expand-Archive -Path “$destinationFolder\archive.zip” -DestinationPath $destinationFolder
Write-Host “Archive extracted successfully to $destinationFolder”
catch
Write-Error “Error downloading or extracting archive: $_”
“`
This improved script handles the download and extraction in a more organized manner. It ensures that the archive is downloaded first before extraction.
Downloading a Large File with Progress, Download file from url powershell
Downloading large files can take time. This example shows how to track progress during the download process using `-Progress`.
“`powershell
$url = “https://www.example.com/largefile.iso”
$destinationFile = “C:\downloads\largefile.iso”
Invoke-WebRequest -Uri $url -OutFile $destinationFile -Progress
“`
The `-Progress` parameter is a powerful tool. It provides real-time feedback, allowing you to monitor the download’s progress, which is critical for large files, preventing frustration and potential errors.
Downloading a File with Authentication
This example demonstrates downloading a file that requires authentication. It utilizes the `-Headers` parameter to include credentials.
“`powershell
$url = “https://secure.example.com/protectedfile.pdf”
$destinationFile = “C:\downloads\protectedfile.pdf”
$username = “yourusername”
$password = “yourpassword”
$basicAuth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(“$username:$password”))
Invoke-WebRequest -Uri $url -OutFile $destinationFile -Headers @Authorization = “Basic $($basicAuth)”
“`
This example uses basic authentication. Remember to replace placeholders with your actual credentials. Consider using a secure method for storing sensitive information like passwords in real-world scenarios.
Automating Multiple File Downloads
This example shows how to automate the download of multiple files from a website using a loop. It showcases robust error handling and efficient script design.
“`powershell
$urls = @(
“https://www.example.com/file1.txt”,
“https://www.example.com/file2.pdf”,
“https://www.example.com/file3.jpg”
)
foreach ($url in $urls)
$filename = Split-Path -Leaf $url
$destinationFolder = “C:\downloads”
$destinationFile = Join-Path -Path $destinationFolder -ChildPath $filename
try
Invoke-WebRequest -Uri $url -OutFile $destinationFile
Write-Host “Downloaded $filename”
catch
Write-Error “Error downloading $filename: $_”
“`
This script demonstrates a structured approach to downloading multiple files from a website, using variables and loops for flexibility. It’s crucial to include error handling for a reliable script.