How to download files from the Web using PowerShell

This shows how to download files from the web via HTTP or HTPPS using Invoke-WebRequest CmdLet.

Download from Web via HTTP or HTTPS

In this example, an HTML page of this website has been downloaded. Invoke-WebRequest syntax can be found from Microsoft here.

$website = "https://techadviz.com"

$source = "$website/privacy-policy"

$destination = "C:\privacy-policy.html"

Invoke-WebRequest $source -OutFile $destination

Run:

Download error from a web requiring Authentication

Along the same lines, if you access a website you do not have permission, an access error is shown.

Run:

Download Method from Web requiring Authentication

Enter the password in the Get-Credential prompt, and continue Invoke-WebRequest with the credential.

$Credentials = Get-Credential

Invoke-WebRequest $source -OutFile $destination -Credential $Credentials

If you already stored your credential in XML, you can use the stored one. How to store in XML can be found.

$credxml = "mycredential.xml" # saved before

$cred = Import-Clixml $credxml

Invoke-WebRequest $source -OutFile $destination -Credential $cred

Leave a Comment