Saturday, November 25, 2006 6:35 AM bart

PowerShell - How to download a file?

Time for a little PowerShell tip for my IT Pro friends. I know it's just BCL (Base Class Library) stuff, but nevertheless you might find it useful: what about downloading a file from an HTTP server during script execution? Here it is:

PS C:\temp> $clnt = new-object System.Net.WebClient
PS C:\temp> $clnt | gm d*


   TypeName: System.Net.WebClient

Name                MemberType Definition
----                ---------- ----------
Dispose             Method     System.Void Dispose()
DownloadData        Method     System.Byte[] DownloadData(String address), S...
DownloadDataAsync   Method     System.Void DownloadDataAsync(Uri address), S...
DownloadFile        Method     System.Void DownloadFile(String address, Stri...
DownloadFileAsync   Method     System.Void DownloadFileAsync(Uri address, St...
DownloadString      Method     System.String DownloadString(String address),...
DownloadStringAsync Method     System.Void DownloadStringAsync(Uri address),...


PS C:\temp> $url = "http://www.bartdesmet.net/download/ps.txt"
PS C:\temp> $file = "c:\temp\ps.txt"
PS C:\temp> $clnt.DownloadFile($url,$file)
PS C:\temp> type $file
Welcome to Windows PowerShell 1.0! 

Drawback to this approach: no download reporting (as with write-progress) while downloading a large file. Solution: coming up later. Enjoy!

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# The November 06 Month Report

Friday, December 01, 2006 4:06 AM by B# .NET Blog

Yet another great (well, at least in my opinion) month of Daily Blogging . Once more, feedback from readers

# newbie.blog » Download Files Natively with PowerShell

Tuesday, July 31, 2007 1:24 PM by newbie.blog » Download Files Natively with PowerShell

Pingback from  newbie.blog » Download Files Natively with PowerShell

# PowerShell script to download a list of files : Philippe

Wednesday, March 04, 2009 10:42 AM by PowerShell script to download a list of files : Philippe

Pingback from  PowerShell script to download a list of files : Philippe

# Log Retrevial Script v 2 « The Scripting Admin

Thursday, June 17, 2010 10:11 AM by Log Retrevial Script v 2 « The Scripting Admin

Pingback from  Log Retrevial Script v 2 « The Scripting Admin

# Native Alternative To Wget In Windows PowerShell? | Click & Find Answer !

Pingback from  Native Alternative To Wget In Windows PowerShell? | Click & Find Answer !