Skip to content

Instantly share code, notes, and snippets.

@dlwyatt dlwyatt/DownloadFile.ps1 Secret
Last active Sep 13, 2018

Embed
What would you like to do?
Write-Progress from WebClient
function Invoke-FileDownload
{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[uri] $Uri,
[Parameter(Mandatory)]
[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
You can’t perform that action at this time.