Saturday, November 25, 2006 6:35 AM
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*
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
Filed under: Windows PowerShell