• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 714
  • Last Modified:

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.
0
sj77
Asked:
sj77
  • 2
1 Solution
 
Jose Gabriel Ortega CCEO J0rt3g4 Consulting ServicesCommented:
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 CCEO J0rt3g4 Consulting ServicesCommented:
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
 
sj77Author Commented:
This worked for me, thanks, Jose!
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now