Export certiticate file by PS

2024-05-16T08:42:55.1233333+00:00

Hi, good morning!

I have a list of 900 certificates in the Certification Authority (folder Issued Certificates). I would like to export each certificate in Base-64 X.509 by script in Power Shell, because in the GUI, I need to do one by one.

I have tried many commands and scripts to do this. All processes show me success, but when I try to open the certificate file, it shows me a message:

Invalid Public Key Object File

  • Thefile is invalid to use the following Security Certificate. Could you support me? Thank in advance
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,418 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 98,631 Reputation points MVP
    2024-05-16T09:05:33.2033333+00:00

    Hi @Oliveira Junior, Jose Ramos (SMO TK RYD SIG) ,

    maybe this helps to get started (haven't tested the script because no CA available at the moment):

    Import-Module ADCSAdministration
    $caName = "Name of CA"
    $outputDir = "C:\Certificates"
    $caObj = Get-CertificationAuthority -Name $caName
    $certs = Get-IssuedRequest -CertificationAuthority $caObj
    foreach ($cert in $certs) {
        $certObj = Get-Certificate -CertId $($cert.RequestID) -CertAuthority $caObj
        [System.IO.File]::WriteAllText($("$outputDir\Cert_$certId.cer"), $certObj.RawData | Convert.ToBase64String)
    }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. 2024-05-16T18:27:28.0733333+00:00

    I used this script, and it is working! Thank for all

    ## Converting PEM String into X509Certificate2 object$rawCertificate = ($pemCertificate -split '\r\n' | Select-Object -Skip 1 | Select-Object -SkipLast 2) -join ''$newCertificate = System.Security.Cryptography.X509Certificates.X509Certificate2.

    0 comments No comments