title: certutil dump - expiration date p12 - powershell
body: <p>I am not really a programmer but I am trying to get fixed a Powershell script that would help me get the expiration date from a P12 certificate on file.</p>
  
  <p>This is the command:</p>
  
  <pre>
     C:\>certutil.exe -dump c:\1\p_CERT.p12
  Certificates: Not Encrypted
  ================ Certificate 0 ================
  ================ Begin Nesting Level 1 ================
  Element 0:
  Serial Number: 03g3
  Issuer: CN=COMPANY MY  CA v2, O=Company SL, C=GL
   NotBefore: 2012-06-20 11:47
   NotAfter: 2022-06-20 11:46
  Subject: CN=COMPANY MY CA v2, O=Company SL, C=GL
  Signature matches Public Key
  Root Certificate: Subject matches Issuer
  Cert Hash(sha1): 1234124214214214214sdada122
  ----------------  End Nesting Level 1  ----------------
  No key provider information
  Cannot find the certificate and private key for decryption.
  
  ================ Certificate 1 ================
  ================ Begin Nesting Level 1 ================
  Element 1:
  Serial Number: 2100
  Issuer: CN=COMPANY MY CA v2, O=Company SL, C=GL
   NotBefore: 2018-12-07 08:48
   NotAfter: 2020-12-07 08:48             
  Subject: CN=private_CERT + SERIALNUMBER=445566778899, O=OTHER_Company, C=GL
  Non-root Certificate
  Cert Hash(sha1): 1234423hhhshshhshsh444423232
  ----------------  End Nesting Level 1  ----------------
    Key Container = PfxContainer
    Provider = PfxProvider
  Encryption test FAILED
  CertUtil: -dump command completed successfully.
  </pre>
  
  <p>The interesting part is the second one, that with the SERIALNUMBER, the "NotAfter: 2020-12-07 08:48"</p>
  
  <p>Using this one as a source of inspiration (<a href="https://gist.github.com/banterCZ/9bd6aa1ab49995fdf018" rel="nofollow noreferrer">https://gist.github.com/banterCZ/9bd6aa1ab49995fdf018</a>), thanks banterCZ, I tried the following. But it is not working as the result is not the field "NotAfter".
  Any thoughts how to get this part out "NotAfter: 2020-12-07 08:48" with the script?</p>
  
  <pre><code>########################################################
  #
  #       Check certificates inside a p12 certificate file
  #
  ########################################################
  
  [CmdletBinding()]
  Param(
      [Parameter(Mandatory=$True)]
      [string]$location,
  
      [Parameter(Mandatory=$True)]
      [int]$threshold
  )
  
  [System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US"
  
  $certutil="certutil.exe"
  $certificate = Invoke-Expression "$certutil -dump '$location'"
  
  foreach($line in $certificate){    
      if($line.Contains("Element 1:")){    
          $index = $line.Substring(0,20)
          write-host $index
          $dateAsString = $line | Select-String -Pattern 'NotAfter' | foreach {$_.groups[""].value}
          write-host $dateAsString
          #$expirationDate = [datetime]::parseexact($dateAsString,"ddd MMM dd HH:mm:ss yyyy",$null)
          break
      }
  }
  
  $now = ([System.DateTime]::Now)
  $daysToExpire = [int]($expirationDate - $now).TotalDays
  
  if ($threshold -lt $daysToExpire) {
      Write-Host "[OK] Certificate '$alias' expires in '$expirationDate' ($daysToExpire day(s) remaining)."
      exit 0
  } elseif ($daysToExpire -lt 0) {
      Write-Host "[CRITICAL] Certificate $alias has already expired."
      exit 2
  } else {
      Write-Host "[WARNING] Certificate '$alias' expires in '$expirationDate' ($daysToExpire day(s) remaining)."
      exit 1
  }
  </code></pre>
  
  <p>thanks!</p>