Failed to download from Internet using Powershell

Published on: 12th May 2020

Updated on: 3rd Aug 2025

Explanation

The following code is downloading a text file from a web server. It looks simple and straightforward.

$u2 = "https://my-test-web.com/my-file.txt"
$f2 = "my-file.txt"
Invoke-WebRequest -Uri $u2 -OutFile $f2

But, we never expect downloads to fail except for when the case of Internet connection has broken down. This can be verified by pasting the URL into any Internet browser and see if the file is accessible. If the browser has downloaded the file, this means there is something wrong with your Powershell script.

If you look at the value of $u2, you might notice that it is accessing HTTPS and Powershell will validate the SSL certificate. The validation involve in determining which Tls version to use. So, there is a chance for the above script fail when the web server has disabled Tls v1.1 and below while your Powershell is using the older Tls version.

To resolve this issue, you must change the Tls version in the environment setting before calling Invoke-WebRequest. This means, you change the TLS to version 1.2 programmatically before the Invoke-WebRequest.

# enforce Tls1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$u2 = "https://my-test-web.com/my-file.txt"
$f2 = "my-file.txt"
Invoke-WebRequest -Uri $u2 -OutFile $f2

That's it for now. But, you might face the same error again with Tls 1.2. When that happened, you may want to use Tls 1.3.

Notes

The Invoke-WebRequest command works like curl in Linux environment. It is meant for downloading some files from the web server and save it to local storage.

Back to #POWERSHELL blog

Back to #blog listing

Author

Lau Hon Wan, software developer.