Powershell 2.0 Download File

The WebClient.DownloadFile method is synchronous and does not display progress in PowerShell 2.0. If you need a progress bar, you cannot use DownloadFile. Instead, you must use WebClient.OpenRead to stream the data manually.

Here is a PowerShell 2.0 progress bar hack:

function Download-FileWithProgress 
    param($url, $outputPath)
$client = New-Object System.Net.WebClient
$stream = $null
$fileStream = $null
try 
    # Get file size for progress calculation
    $client.OpenRead($url) 
finally 
    if ($stream)  $stream.Close() 
    if ($fileStream)  $fileStream.Close() 
    $client.Dispose()

while ($webClient.IsBusy) Start-Sleep -Milliseconds 500

Note: Event handling in PS 2.0 can be clunky. For simple scripts, stick to the synchronous method.

Here is the skeleton code to download a file from an HTTP/HTTPS endpoint to your current directory: powershell 2.0 download file

# PowerShell 2.0 - Basic File Download
$url = "https://www.example.com/software/update.msi"
$output = "$pwd\update.msi"

$webClient = New-Object System.Net.WebClient $webClient.DownloadFile($url, $output)

Write-Host "Download completed to: $output" The WebClient

How it works: