Skip to content

Instantly share code, notes, and snippets.

@akanieski
Created November 7, 2023 21:25
Show Gist options
  • Save akanieski/3eaa387dff49e648831b57f42b17c64d to your computer and use it in GitHub Desktop.
Save akanieski/3eaa387dff49e648831b57f42b17c64d to your computer and use it in GitHub Desktop.
Export-WindowsCertStoreToPEM
# Get all certificates from the cert store
$certs = Get-ChildItem -Path "Cert:\CurrentUser\Root"
# Initialize an empty array to hold the PEM strings
$pemCerts = @()
# Loop over each certificate
foreach ($cert in $certs) {
# Export the certificate as a PEM string
$pem = [System.Convert]::ToBase64String($cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert))
# Add the PEM string to the array
$pemCerts += "# $($cert.Subject)"
$pemCerts += "-----BEGIN CERTIFICATE-----`n$pem`n-----END CERTIFICATE-----"
}
# Join the PEM strings into a single string
$pemFileContent = $pemCerts -join "`n"
# Write the PEM file
Set-Content -Path "certs.pem" -Value $pemFileContent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment