Help needed with script (Certification Authority)

Hello everyone,


I'm relatively new to PS. I'm working on a script which mails a list of certificates, which expire within 30 days.

The problem is, I think, he compares the "Certificate Expire Date" in the wrong format. He gives me a list where the expiration date is in the format MM/dd/yyyy, when both my pc and server are in format dd/MM/yyyy.

Can someone help me sort this out?

Here's the script:
# variables
$datestring = (Get-Date).ToString(“yyyyMMdd”).Replace(“:”,”-”)
$filesave = “d:\CertMGMT\CertExport_$datestring.csv”
[string]$caServerName = "CA-location"
[string]$caCertExportPath = "$filesave"
[string]$smtpSender = ""
[string]$smtpRecipient = ""
[string]$smtpServer = "Mailserver"
[int]$daysUntilExpiry = 30
$expiringCerts = @()

function Send-EmailCertNotice ([string]$_certificateList) {
$MailMessage = @{
    To = $smtpRecipient
    From = $smtpSender
    Subject = @"
The following issued digital certificates will expire soon.
    building = @"
The following digital certificates issued by (company) will expire in the next $daysUntilExpiry days.
Please send a request for a certificate replacement/renewal to the users, if the following certificates are still needed.
NOTE: This notification is being sent by an automated certificate management process and
cannot receive reply e-mail. If you have any questions please contact your system administrator.
    Smtpserver = $smtpServer
    BodyAsHtml = $false
    ErrorAction = "SilentlyContinue"
Send-MailMessage @MailMessage
# export certs to CSV file
certutil -view -restrict "Public Key Length=2048,Request Disposition=20" -config $caServerName csv > $caCertExportPath
# load cert CSV into an array
$issuedCerts = Import-Csv $caCertExportPath
if ($issuedCerts.Length -gt 0) {
    foreach ($cert in $issuedCerts) {
        try {
            $certExpires = [datetime]$cert."Certificate Expiration Date"
            $cert."Certificate Expiration Date" = $certExpires
        catch [Exception] {
        if ($certExpires -gt $(Get-Date) -and $certExpires -lt $(Get-Date).AddDays($daysUntilExpiry)) {
# filter out revoked and only smartcarduser certs.
            if ($cert."Issued Organization Unit" -ne "EMPTY"
                ) {
                $expiringCerts += $cert
    $bodyVal = $expiringCerts | Select-Object @{n="Certificate ID"; e="Request ID"},
    "Certificate Expiration Date", "Issued Common Name" | Sort-Object { $_."Certificate Expiration Date" -as [datetime] } | Out-String
    Send-EmailCertNotice $bodyVal

No Data
  • Hi,

    I'm hoping someone can help me get this script to work - i'm having the same problem as Christof - the date format is correct is the exported CSV - dd-mm-yyyy but in the mail it is reversed.

    I looked at the solution that nohandle has suggested but I cannot understand how to insert it in to the script.

    I'm a complete novice with Powershell and I was hoping someone could help with the original script so that the fix nohandle has suggested will be included.

    Many thanks in advance.


No Data