Download a File From a Website with PowerShell

Introduction

So by now you have figured out that PowerShell is an excellent tool for working on the local system and automating local tasks. This tutorial will show you how to download files from an external site.

Why would we do this?

Well maybe we have a looping script that constantly needs fresh data supplied by an external source. The realm of possibilities is greatly expanded by knowing how to download external data in PowerShell.

HTTP

Example


$source = "http://yoursite.com/file.xml"
$destination = "c:\application\data\newdata.xml"
 
Invoke-WebRequest $source -OutFile $destination
    

Copy and Try it

The Invoke-WebRequest cmdlet

Invoke-WebRequest is a cmdlet that lets you upload or download data from a remote server. This cmdlet allows for user agents, proxies, and credentials.

FTP

Example

$source = "ftp://yoursite.com/file.xml"
$destination = "c:\application\data\newdata.xml"
 
Invoke-WebRequest $source -OutFile $destination -Credential ftpUser
    

Copy and Try it

The code example above is almost identical to the HTTP sample, with the main difference being that the $source variable has "ftp" at the beginning instead of "http". You may also notice that we have used the -Credential parameter since FTP connections generally require a username and password.

And that's it? Yep, it's that easy to download data from web sites using Powershell, and we even get two different protocols to choose from.