Hi All,
Good day! I'm working on an initiative where part of the requirements is to get the properties (such as Issuer, NotAfter, Thumbprint, etc) of a certificate file. I tried the following methods but I wasn't able to achieve what I need. Appreciate your inputs in case you have worked with the same requirement before or have an idea how to implement it.
Best regards,
Jay

Method 01 : Run VBScript
Script
' Create an instance of the X509Certificate2 class
Set cert = CreateObject("X509Certificate2")
' Load the certificate from a file or other source
cert.Import("<Folder Path>\SampleCert_1.cer")
' Check if the certificate is valid
If cert.IsValid Then
' Retrieve the certificate properties
CommonName = cert.SubjectName.Name
Issuer = cert.IssuerName.Name
SerialNumber = cert.SerialNumber
Thumbprint = cert.Thumbprint
ValidFrom = cert.NotBefore
ValidTo = cert.NotAfter
' Display the certificate properties
WScript.Echo "Common Name: " & CommonName
WScript.Echo "Issuer: " & Issuer
WScript.Echo "Serial Number: " & SerialNumber
WScript.Echo "Thumbprint: " & Thumbprint
WScript.Echo "Valid From: " & ValidFrom
WScript.Echo "Valid To: " & ValidTo
Else
' Certificate is not valid
WScript.Echo "Invalid certificate"
End If
' Clean up
Set cert = Nothing
Result
Script returned an error <Folder Path>\AppData\Local\Temp\Robin\wt1xx2uaddz.tmp(6, 1) Microsoft VBScript runtime error: ActiveX component can't create object: 'X509Certificate2'
Method 02 : Run PowerShell Script
Script
$CertificatePath = "<Folder Path>\SampleCert_1.cer"
try {
# Load the certificate from the specified path
$certificate = Get-ChildItem -Path $CertificatePath | Select-Object -First 1
# Check if the certificate is valid
if ($certificate -eq $null) {
throw "Certificate not found at the specified path."
}
# Retrieve the certificate properties
$properties = @{
Subject = $certificate.Subject
Issuer = $certificate.Issuer
NotBefore = $certificate.NotBefore
NotAfter = $certificate.NotAfter
Thumbprint = $certificate.Thumbprint
}
# Output the certificate properties
Write-Output $properties
}
catch {
# Log and handle any errors
Write-Error $_.Exception.Message
}
Result
Script returned below output but values are empty though available when directly checked from certificate details.
Name Value
---- -----
Subject
Issuer
NotBefore
Thumbprint
NotAfter