How do I export a certificate using Base 64 .CER format with PowerShell ?
The Export-Certificate cmdlet has a 'Type' parameter with a P7B value, but I'm not sure if that's the same as selecting the 'Base-64 encoded X.509 (.CER)' radio button in the 'Certificate Export Wizard' using the GUI (see screenshot below)
P7B is binary bundle of certificates which is not what you're looking for. Unfortunately, the Export-Certificate cmdlet does not offer the "Base-64 encoded X.509 (.CER)" type to be exported but you can use below snippet to get the job done.
I found that 'certutil -encode' can also be used for exporting to Base64 format.
I noticed that exporting to Base64 format using both 'certutil -encode' and the MMC Certificate GUI adds the 'BEGIN/END CERTIFICATE' tags, and adds line breaks after 65 characters. And with ToBase64String, the InsertLineBreaks parameter adds line breaks after 76 characters, and the 'BEGIN/END CERTIFICATE' tags need to be hand-coded.
I know the line breaks shouldn't matter, but just to retain compatibility with the native Windows way in which Base64 certificates are exported, I ended up using the following code: