2

TL;DR: Need better way to implement progressbar for Powershell File download.

I have a .PS1 script with a GUI that I converted to a EXE using PS2EXE. I have a piece of code that downloads a large zipfile (~4GB) from the internet.

Start-BitsTransfer -Source http://speedtest.tele2.net/100MB.zip -Destination .\100MB.zip

Problem: When its a PS1 file, output looks okay. Performance is great, user can close window to cancel the progress. However, when converting to PS2EXE, user cannot cancel the command (there is 'close window' or cancel button. And MainForm is unresponsive till command is complete or fails). Additionally, there is no way to add ETA using Bits-transfer, especially with PS2EXE.

PS2EXE is compiled using -noconsole -nooutput -noError because I dont want the output on the main script.

For a large file (4GB), it can take anywhere from 2-20mins, depending on internet speeds. I'd like to implement a solution that can:

  1. Implement a cancel button/close window
  2. I get to keep the progressbar
  3. Has to be quick (Invoke-WebRequest is too slow, bits-transfer and WebClient is fast)
  4. Optional: gives option to add additional output (ETA information in seconds)

enter image description here

enter image description here

What I've tried so far:

  1. Invoke-WebRequest/Invoke-RestMethod (too slow because of the way it implements the progress bar)
  2. WebClient.Downloadfile($url, $outfile) (cannot easily implement progressbar)
  3. start-process -PassThru -wait powershell {Start-BitsTransfer -Source http://speedtest.tele2.net/100MB.zip -Destination .\100MB.zip} to execute a new powershell process to execute the bits-transfer with progressbar (need a more elegant solution if there is one. This is currently the best implementation I have)

By more elegant I mean using a (PS2EXE) GUI instead of a powershell window.

Bonus question: How do I get the ETA (00:00:10 sec of 00:02:20 sec) information from bits-transfer?

nmpereira
  • 43
  • 3
  • 2
    `WebClient.DownloadFileAsync()` (it's event-driven) has a progress event that you can subscribe to. But the same applies to `WebClient.DownloadFileTaskAsync()` (actually async this one). The `DownloadProgressChanged` event is commonly used to update the UI on the progress. – Jimi Apr 21 '21 at 21:38

0 Answers0