Downloads with Progress Bar

by Dec 2, 2008

If you'd like to download larger files from the Internet and get a progress indicator, you can load the .NET Visual Basic assemblies, which provide a sophisticated download method with progress bar:

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$url = 'http://download.microsoft.com/download/4/7/1/47104ec6-410d-4492-890b-2a34900c9df2/Workshops-EN.zip'
$local = "$homepowershellworkshop.zip"
$object = New-Object Microsoft.VisualBasic.Devices.Network
$object.DownloadFile($url, $local, '', '', $true, 500, $true, 'DoNothing')

In case you are wondering what kind of arguments DownloadFile takes, look up the method. It's easy:

[System.Diagnostics.Process]::Start( `
'http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.devices.network.downloadfile.aspx')

The previous line used a static .NET method called Start(). Static methods do not come from a specific object, so you don't need New-Object. Instead, they are generic. Start() provided by the System.Diagnostics.Process class launches any command or file just as if you had pressed (WIN)+(R) and entered it into the Run dialog box. So you can use Start() to open Web pages in your default browser.