Quick way to download file using PowerShell

Fastest way to download file using PowerShell is by using WebClient.DownloadFile method from .NET class System.Net.WebClient used for downloading files from Web.

You only need to provide source and destination where the downloaded file should be put. Example below shows how to use it. You can make it a "one liner" by putting values of $url and $outputFile directly in DownloadFile method call.

$url = "http://releases.ubuntu.com/18.04/ubuntu-18.04.1-desktop-amd64.iso"
$outputFile = "c:\iso\ubuntu-18.04.1-desktop-amd64.iso"
(New-Object System.Net.WebClient).DownloadFile($url, $outputFile)

If you get error like the one shown below, you have to check if folders in $outputFile exist.

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:1 char:1
+ (New-Object System.Net.WebClient).DownloadFile($url, $outputFile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

Leave a Comment