PowerShell public

Windows Certificate Expiry

Share this script safely, inspect its metadata, and copy the exact command you need.

Dashboard

Ready to run

One-liner

Copy the command, then review before execution.
curl -fsSL https://runny.sh/r/KD72ZVQ7AE4M | pwsh

Current version: 1.1 · Updated by Godmode · 2026-09-07 02:38:59.598

At a glance

Script metadata

Slug
KD72ZVQ7AE4M
Fetches
0
Size
2039 B · 75 lines
Expires
never
Last fetch
Created
2026-09-07 00:36:00.978
Description
LocalMachine My store certs nearing expiry

Readable before runnable

Preview

# Windows certificate expiry
# Read-only: LocalMachine\My certificates expiring within 30 days.
# Exit 0 if current, 1 if a cert expires soon, 2 if already expired or the store cannot be read.
# Hosted on Runny.sh. Review the script page before you run it.

$ErrorActionPreference = 'Continue'
$status = 0
$soon = 0
$expired = 0

function Write-Line {
    param([string]$Text)
    Write-Output $Text
}

function Set-Warn {
    if ($script:status -lt 1) { $script:status = 1 }
}

function Set-Crit { $script:status = 2 }

Write-Line '=== LocalMachine\My certificate expiry ==='
Write-Line ("Host:     {0}" -f $env:COMPUTERNAME)
Write-Line ''

try {
    $certs = @(Get-ChildItem -Path 'Cert:\LocalMachine\My' -ErrorAction Stop)
} catch {
    Write-Line 'Could not read Cert:\LocalMachine\My'
    Write-Line $_.Exception.Message
    Write-Line 'Result:   ERROR'
    exit 2
}

$now = Get-Date
$horizon = $now.AddDays(30)
Write-Line ("Certs:    {0}" -f $certs.Count)

if ($certs.Count -eq 0) {
    Write-Line '  store is empty'
    Write-Line '          OK'
} else {
    foreach ($cert in $certs) {
        $name = $cert.Subject
        $until = $cert.NotAfter
        $days = [int]($until - $now).TotalDays
        if ($until -lt $now) {
            Write-Line ("  EXPIRED  {0:yyyy-MM-dd}  {1}" -f $until, $name)
            $expired++
            Set-Crit
        } elseif ($until -le $horizon) {
            Write-Line ("  {0}d left  {1:yyyy-MM-dd}  {2}" -f $days, $until, $name)
            $soon++
            Set-Warn
        }
    }
    if ($expired -eq 0 -and $soon -eq 0) {
        Write-Line '  none expiring within 30 days'
        Write-Line '          OK'
    }
}

Write-Line ''
if ($status -eq 0) {
    Write-Line 'Result:   CURRENT'
} elseif ($status -eq 1) {
    Write-Line ("Result:   ACTION NEEDED ({0} cert(s) expire within 30 days)" -f $soon)
} elseif ($expired -gt 0) {
    Write-Line ("Result:   ACTION NEEDED ({0} expired, {1} expiring soon)" -f $expired, $soon)
} else {
    Write-Line 'Result:   ERROR'
}

exit $status

Raw endpointhttps://runny.sh/r/KD72ZVQ7AE4M

See something unsafe?

Report this script

Tell us why it should come down. Anyone can also report a URL at /abuse.

Restore revision