Invoke-WebRequest and Invoke-RestMethod failed with SSL/TLS

When running the Invoke-WebRequest or Invoke-RestMethod command, you may encounter this error:

The request was aborted: Could not create SSL/TLS secure channel.
At line:1 char:1
+ Invoke-WebRequest https://xxxxx …
>+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

By default PowerShell uses TLS 1.0 and the remote ressource is configured ito use TLS 1.2. To tell PowerShell to use TLS 1.2, you need to run:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Note: Only the current PowerShell session (PowerShell window) will be in TLS 1.2, you must therefore execute this command each time you open the PowerShell window or put it in your PowerShell profile.

If you know how to configure PowerShell to use TLS1.2 by default, please say it in comments.

Leave a Reply

Close Menu