Sunday, November 16, 2014

PowerShell one liner to download a file from a URL

PowerShell 3 and 4 include the Invoke-WebRequest (wget) to download a file from a URL.

A PowerShell 4 one liner to download a file from a URL is:

Invoke-WebRequest url -OutFile filename

Replace url with a string that has the full URL for the file and replace filename with a string containing the local file name.  For example, to download get-pip.py I could do the following:

Invoke-WebRequest "https://raw.github.com/pypa/pip/master/contrib/get-pip.py" -OutFile "get-pip.py"

 In PowerShell 2 you can use the following one liner to achieve the same.

(New-Object System.Net.WebClient).DownloadFile(url,filename)

For example, to download get-pip.py I could do the following:

(New-Object System.Net.WebClient).DownloadFile("https://raw.github.com/pypa/pip/master/contrib/get-pip.py","get-pip.py")

Optionally, if you are running Windows 7 you could switch  to PowerShell 4 by installing Windows Management Framework 4.0.