Windows admin PowerShell

Check certificate expiry dates in the local machine store

List the certificates in the local machine store that have expired or will expire within a number of days, with the days left for each.

LanguagePowerShell
What it does to the systemReads only. It changes nothing.

An expired certificate stops a website, a management point or a remote desktop logon with little warning. The date is easy to check ahead of time.

This script reads the certificates in the local machine store. It lists the ones that have expired or will expire soon, with the days left.

PowerShell
<#
  Certificates in the local machine store that expire soon.
  Run it on the computer. It only reads.
  Example: .\Get-ExpiringCertificate.ps1 -Days 90
#>
param(
    # Show certificates that expire within this many days. Expired ones always show.
    [int]$Days = 60,

    # My = the Personal store. Use Root, CA or WebHosting for other stores.
    [string]$StoreName = 'My',

    # Show every certificate, not only the ones that expire soon.
    [switch]$All
)

$now = Get-Date
$limit = $now.AddDays($Days)

Get-ChildItem -Path "Cert:\LocalMachine\$StoreName" -ErrorAction Stop |
    Where-Object { $_.NotAfter -and ($All -or $_.NotAfter -le $limit) } |
    Sort-Object NotAfter |
    ForEach-Object {
        $daysLeft = [int][math]::Floor(($_.NotAfter - $now).TotalDays)
        $status = 'OK'
        if ($daysLeft -lt 0) { $status = 'Expired' }
        elseif ($_.NotAfter -le $limit) { $status = 'Expires soon' }
        [pscustomobject]@{
            Status     = $status
            DaysLeft   = $daysLeft
            NotAfter   = $_.NotAfter
            Subject    = $_.Subject
            Issuer     = $_.Issuer
            Thumbprint = $_.Thumbprint
        }
    }

How to use it

  1. Save the script as Get-ExpiringCertificate.ps1.
  2. Run .\Get-ExpiringCertificate.ps1 on the server. You do not need administrator rights to read the store.
  3. Change -Days to look further ahead, for example -Days 90.
  4. Add -All to list every certificate in the store.
  5. For a remote server, run Invoke-Command -ComputerName SRV01 -FilePath .\Get-ExpiringCertificate.ps1.

Good to know

  • No output means nothing in the store expires within the period.
  • The Personal store holds the certificates that IIS, remote desktop and the SCCM client use.
  • An expired certificate in the Root store is normal. Windows keeps old root certificates.
certificates expiry Cert drive LocalMachine NotAfter
✉

Get new articles in your inbox

SCCM guides, AI tools, automation tips — sent when published. No spam.