Downloading Files from Internet

by Mar 23, 2022

Invoke-WebRequest can’t just send requests to webservices. This cmdlet can communicate with remote systems and transfer data back and forth. That’s why you can use it to download files from the internet in a very simple and straight-forward way:

$url = 'https://www.nasa.gov/sites/default/files/thumbnails/image/iss065e009613.jpg'
$destination = "$env:temp\picture_nasa.jpg"

Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $destination 


Invoke-Item -Path $destination

The first part downloads the NASA picture and saves it locally, and the next part opens the downloaded image in your default viewer.

Note that Invoke-WebRequest may not work with older network and TLS protocols.

Here are two more things to note:

  • -UseBasicParsing prevents the cmdlet from using the old and deprecated “Internet Explorer” object model which can cause issues these days. It used to be useful to parse raw HTML from websites using the IE libraries.
  • Invoke-WebRequest has a big brother called Invoke-RestMethod. Both work the same, yet Invoke-RestMethod automatically converts downloaded data into the proper format, i.e. XML or JSON. For simple binary downloads like in the example here, this is not necessary.

Twitter This Tip! ReTweet this Tip!