Download a file with Powershell

If you need to download a file with Powershell you can utilise the System.Net.WebClient class. The WebClient class contains a method called DownloadFile which allows you to specify a source and a destination parameter. An example of this is below:

$wc = New-Object System.Net.WebClient

$source = "http://example.com/file.txt"
$dest = "c:\test\file.txt"

$wc.DownloadFile($source, $dest)

I came across this example in The Windows PowerShell Cookbook by O’Reilly (Amazon US,  UK). I recommend you get a copy as it contains many useful snippets.

Jason Edwards