Is there any way to monitor the progress of a download using a WebClient object in powershell? Is there any way to monitor the progress of a download using a WebClient object in powershell? powershell powershell

Is there any way to monitor the progress of a download using a WebClient object in powershell?


To display a progress bar for downloading files check out Jason Niver's Blog post:

Downloading files from the internet in Power Shell (with progress)

Basically you can create a function that still uses the web client functionality but includes a way to capture the status. You can then display the status to the user using theWrite-Progress Power shell functionality.

function DownloadFile($url, $targetFile){   $uri = New-Object "System.Uri" "$url"   $request = [System.Net.HttpWebRequest]::Create($uri)   $request.set_Timeout(15000) #15 second timeout   $response = $request.GetResponse()   $totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)   $responseStream = $response.GetResponseStream()   $targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create   $buffer = new-object byte[] 10KB   $count = $responseStream.Read($buffer,0,$buffer.length)   $downloadedBytes = $count   while ($count -gt 0)   {       $targetStream.Write($buffer, 0, $count)       $count = $responseStream.Read($buffer,0,$buffer.length)       $downloadedBytes = $downloadedBytes + $count       Write-Progress -activity "Downloading file '$($url.split('/') | Select -Last 1)'" -status "Downloaded ($([System.Math]::Floor($downloadedBytes/1024))K of $($totalLength)K): " -PercentComplete ((([System.Math]::Floor($downloadedBytes/1024)) / $totalLength)  * 100)   }   Write-Progress -activity "Finished downloading file '$($url.split('/') | Select -Last 1)'"   $targetStream.Flush()   $targetStream.Close()   $targetStream.Dispose()   $responseStream.Dispose()}

Then you would just call the function:

downloadFile "http://example.com/largefile.zip" "c:\temp\largefile.zip"

Also, here are some other Write-Progress examples from docs.microsoft for powrshell 7.

Write-Progress


In V2 you could just use the BitsTransfer module e.g.:

Import-Module BitsTransferStart-BitsTransfer https://www.example.com/file C:/Local/Path/file