San Tran

an IT System Engineer

PowerShell

Remove Expired Certificates on Windows Machine

I use this script to remove expired certificates in the Local Computer\Personal store.

$store = New-Object System.Security.Cryptography.x509Certificates.x509Store("My","LocalMachine")
$store.Open("ReadWrite")
$certs = $store.Certificates

# Remove all expired certificates
foreach($cert in $certs){
    if($cert.NotAfter -lt (Get-Date)){
        $store.Remove($cert)
    }
}

# Close the cert store
$store.Close()