Display SSL certificate expiration dates via PowerShell

Jess13777 41 Reputation points
2022-06-16T20:49:16.917+00:00

Hello! I'm trying to figure out how to display when SSL certificates expire. Currently, I have the following code (which was provided to me by a colleague) and I want to know how to display the certificate's expiration date. If possible I would also like to know if there is a way to display certificates that expire in a certain time frame (i.e. show SSL certificates expiring between June 1st to July 1st, or sho certificates that only expire on June 25th). Thank you so much for your help in advance!

# Get a list of certs  
dir Cert:\LocalMachine\My  
  
# Grab the thumbprint for the cert we want to use  
$certHash = "D3A6E7B1746DFA37D4B93263AAA1348A2BA41720"  
  
# Get the AppID for the existing app on the interface  
netsh http show sslcert  
$guid = "5d8e2743-ef20-4d38-8751-7e400f200e65"  
  
$ip = "0.0.0.0" # This means all IP addresses  
$port = "443" # the default HTTPS port  
#"http update sslcert ipport=$($ip):$port certhash=$certHash appid={$guid}" | netsh  
Windows development Internet Information Services
Windows for business Windows Server User experience PowerShell
Windows for business Windows Server User experience Other
Windows for business Windows Client for IT Pros User experience Other
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-06-16T22:43:48.56+00:00

    You can use this as a starting point for checking the explicit dates, or range of cert expiration dates, in a script:

    $certHash = "D3A6E7B1746DFA37D4B93263AAA1348A2BA41720"  
    Get-ChildItem -Path cert:\LocalMachine\My -Recurse |   
        Where-Object {$_.Thumbprint -eq $cert} |  
            Select-Object NotAfter  
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-06-20T08:29:48.457+00:00

    Hi there,

    To find certificates that will expire in the next 30 days on all domain servers, use this PowerShell script:

    $servers= (Get-ADComputer -LDAPFilter "(&(objectCategory=computer)(operatingSystem=Windows Server*) (!serviceprincipalname=MSClusterVirtualServer) (!(userAccountControl:1.2.840.113556.1.4.803:=2)))").Name
    $result=@()
    foreach ($server in $servers)
    {
    $ErrorActionPreference="SilentlyContinue"
    $getcert=Invoke-Command -ComputerName $server { Get-ChildItem -Path Cert:\LocalMachine\My -Recurse -ExpiringInDays 30}
    foreach ($cert in $getcert) {
    $result+=New-Object -TypeName PSObject -Property ([ordered]@{
    'Server'=$server;
    'Certificate'=$cert.Issuer;
    'Expires'=$cert.NotAfter
    })
    }
    }
    Write-Output $result

    ------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer–

    4 people found this answer helpful.

  2. S S 0 Reputation points
    2025-02-12T20:29:32.6533333+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.