Skip to content

Instantly share code, notes, and snippets.

Last active March 30, 2022 11:59
What would you like to do?
Write-Progress from WebClient
function Invoke-FileDownload
param (
[uri] $Uri,
[string] $OutputFile
$webClient = New-Object System.Net.WebClient
$changed = Register-ObjectEvent -InputObject $webClient -EventName DownloadProgressChanged -Action {
Write-Progress -Activity "Downloading $Uri to $OutputFile" -PercentComplete $eventArgs.ProgressPercentage
$handle = $webClient.DownloadFileAsync($Uri, $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutputFile))
while ($webClient.IsBusy)
Start-Sleep -Milliseconds 10
Write-Progress -Activity "Downloading $Uri to $OutputFile" -Completed
Remove-Job $changed -Force
Get-EventSubscriber | Where SourceObject -eq $webClient | Unregister-Event -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment