Home > other >  Get certificate expiration date from certificate in file system
Get certificate expiration date from certificate in file system

Time:09-10

I'm using Windows PowerShell script to compare the expiration date of 2 certificates, 1 stored in the certificate store and 1 stored in my file system (C:\inetpub).

However, when i test the output i can't get the expiration date from the certificate that's not in the certificate store. It just shows a blank space when i use Write-Output to check it. Maybe my syntax is wrong or maybe its not possible?

This is what I'm currently trying:

$certName = "websitename.com"
$exportPath = "C:\"
$filePathCRT = $exportPath   $certName   ".crt"
$certSource = "CN="   $certName

$srcCertFile = Get-ChildItem Cert:\LocalMachine\WebHosting | where{$_.Subject -eq $certSource}
if (!$srcCertFile)
{
    Exit
}

$desCertFile = Get-ChildItem Cert:\>C: -Path $filePathCRT
if (!$desCertFile)
{
    Write-Output "cannot get cert file"
}

if ($srcCertFile.NotAfter -ne $desCertFile.NotAfter)
{
    Write-Output "certs not the same"
    Write-Output $desCertFile.NotAfter
}

Does the certificate have to be in the certificate store to get its expiration date?

CodePudding user response:

you can do:

$path = [path]
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($path)
$cert.NotAfter
  • Related