Powershell Script - Download File and Show Progress Bar

Hello, Experts!

I am needing a script help with showing a progress bar when downloading a big file.

Here is my script:

Invoke-WebRequest -Uri https://url/to/file/filename.7z -OutFile C:\temp\NLB.7z

Open in new window


I would like for when the download kicks off it shows a progress bar in the output window as to how far you are in the download.
sj77Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
I think is not an async call, so it basically blocked your console where you run it.

$uri= "https://download.teamviewer.com/download/TeamViewer_Setup.exe"
$out= "F:\TeamViewer_Setup.exe"

[System.Net.WebClient]$wc = new-object System.Net.WebClient
$wc.DownloadFileAsync($uri,$out)

Open in new window

this will create a background job that can be accessed using the regular event handling for .NET
Described here:
https://learn-powershell.net/2013/02/08/powershell-and-events-object-events/
Using that logic it can be archived the progress bar

You can subscribe these events and should register using PS commands.
$wc.DownloadProgressChanged
$wc.DownloadDataCompleted
0
Jose Gabriel Ortega CastroCEOCommented:
I got it to work like this.
$uri= "https://download.teamviewer.com/download/TeamViewer_Setup.exe"
$out= "F:\TeamViewer_Setup.exe"
 $Global:isDownloaded = $false
[int]$percent=0
[int]$totalBytes =0
[int]$receivedByte =0

#Invoke-WebRequest -Uri $uri -OutFile $out


Get-EventSubscriber | Unregister-Event
Get-Job | Remove-Job -Force

[System.Net.WebClient]$wc = new-object System.Net.WebClient
$wc.DownloadFileAsync($uri,$out)


Register-ObjectEvent -InputObject $wc -EventName DownloadFileCompleted -SourceIdentifier Web.DownloadFileCompleted -Action {    
    $Global:isDownloaded = $True
} 

Register-ObjectEvent -InputObject $wc -EventName DownloadProgressChanged -SourceIdentifier Web.DownloadProgressChanged -Action {
    $Global:Data = $event
} 


While (-Not $isDownloaded) {
    $percent = $Global:Data.SourceArgs.ProgressPercentage 
    $totalBytes = $Global:Data.SourceArgs.TotalBytesToReceive
    $receivedBytes = $Global:Data.SourceArgs.BytesReceived 

    $receivedBytes/= 1kb
    $totalBytes/= 1kb
    If ($percent -ne $null) {
        Write-Progress -Activity ("Downloading {0} from {1}" -f $out,$uri) -Status ("{0} kbytes \ {1} kbytes" -f $receivedBytes,$totalBytes)  -PercentComplete $percent
    }
}

Open in new window

And just remember to mark the answers to close the question.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sj77Author Commented:
This worked for me, thanks, Jose!
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.