Trusting Self-Signed HTTPS Certificates

by Mar 10, 2021

If you need to access HTTPS websites that use a self-signed test certificate or a certificate that has expired or is otherwise not trustworthy, PowerShell would refuse to connect. That’s good for most cases, but occasionally you know that the destination server is safe.

Here is some PowerShell code that trusts all HTTPS certificates by overriding the certificate policy. A new certificate policy always returns $true and essentially trusts any certificate:

class TrustAll : System.Net.ICertificatePolicy 
{
  [bool]CheckValidationResult([System.Net.ServicePoint]$sp, [System.Security.Cryptography.X509Certificates.X509Certificate]$cert, [System.Net.WebRequest]$request, [int]$problem)
  {
    return $true
  }
}

[System.Net.ServicePointManager]::CertificatePolicy = [TrustAll]::new()


Twitter This Tip! ReTweet this Tip!