MicrosoftWindows

Downloading files via PowerShell

I have long used WGET to download files from the internet, it has tons of options and made downloading in the background easy, large files, lots of files, incremented names, and even entire websites.
I recently ran into a problem where WGET would download a file, and then hang without finishing. If I used CTRL+C, the download finished and the file was usable, but it was a pain so I started looking to see what else there may be. (NOTE: it turned out to be a version issue, downloading the latest build fixed it).

I came across a post of using PowerShell to download files, and it was easy and flexible:

$client = new-object System.Net.WebClient
$client.DownloadFile(“http://www.xyz.net/file.txt”,”C:tmpfile.txt”)

Another requirement for me is to use a proxy when I’m at work, a little more searching and I found http://powershell.com/cs/media/p/802.aspx with the below example:

# .NET class 
$webClient = new-object System.Net.WebClient 
# specify your proxy address and port 
$proxy = new-object System.Net.WebProxy “proxy.MyDomain.com:8080”  
# replace your credential by your domain, username, and pasword 
$proxy.Credentials = New-Object System.Net.NetworkCredential (“DomainUserName”, “Password”) 
$webclient.proxy=$proxy  
# specify an header if you want to check info in your logs on your proxy 
$webClient.Headers.Add(“user-agent”, “Windows Powershell WebClient Header”) 
# File to download 
$url = “http://download.microsoft.com/download/4/7/1/47104ec6-410d-4492-890b-2a34900c9df2/Workshops-EN.zip” 
# file path on your local drive 
$localfilename = “c:tempWorkshops-EN.zip” 
# action !!!!  
$Webclient.DownloadFile($url, $localfilename) 

Now I can download files through my proxy, and automate the process to allow me to download multiple files simultaneously.

Leave a Reply