Use PowerShell with SSL client certificates for HTTPS GET requests

Sometimes you have use a specific TLS/SSL certificate or thumbprint for outgoing HTTPS connections because of endpoint restrictions. To test these connections you can use PowerShell, but how do you get the required certificate from your certificate store?
Published on Thursday, 2 November 2023

PowerShell 5.0 logo

If an endpoint has restricted HTTPS connections based on security certificates or certificate thumbprints, you must be able to pull that certificate from your Windows Certificate Store and use it in your request. This is not a big issue if the certificate is stored in the CurrentUser store, but it becomes harder when it's in LocalComputer.

How to use an SSL certificate from CurrentUser certificate store in PowerShell Invoke-WebRequest

Current User Certificate Store Management Console

Use certmgr.msc on your command line to open up the CurrentUser certificate store in Managment Console.

If you need to use a personal TLS/SSL certificate you can pull it up using PowerShell:

$certStorePath = "Cert:\CurrentUser\My"
# Must return one result:
$certhash = (
  Get-ChildItem -Path $certStorePath | Where-Object {
    $_.Subject -like "CN=Part_of_Common_Name*"
  }
).Thumbprint

Because Invoke-WebRequest will only look in the CurrentUser certificate store, this will suffice for your HTTPS request. Try:

Invoke-WebRequest -Uri https://example.com -CertificateThumbprint $certThumbPrint

Do you need a LocalMachine certificate, then it becomes a (small) bit harder.

Install SSL/TLS certificates in Windows Server using PowerShell

LocalMachine SSL certificate to use in PowerShell Invoke-WebRequest HTTPS requests

Local Machine Certificate Store Management Console

Start LocalMachine certificate store management console directly with certlm.msc.

Because Invoke-WebRequests only looks at CurrentUser certificate store you a different approach for SSL certificates stored in LocalMachine:

Use Get-ChildItem to get the certificate itself, and pass that to Invoke-WebRequest instead of a thumbprint:

$certStorePath = "Cert:\LocalMachine\My"

# Must return one result:
$certificate = Get-ChildItem -Path $certStorePath | Where-Object {
  $_.Subject -like "CN=Part_of_Common_Name*"
}

Invoke-WebRequest -Uri https://example.com -Certificate $certificate