How do I get the public key from a PFX certificate using Powershell?

Nathan Moinvaziri picture Nathan Moinvaziri · Jul 1, 2016 · Viewed 11.7k times · Source

I am trying to extract the public key from a certificate using Powershell. However, whenever I use the Powershell command Get-PfxCertificate it only outputs the Thumbprint of the certificate and not the public key. How can I get it to output the public key?

Answer

Nathan Moinvaziri picture Nathan Moinvaziri · Jul 1, 2016

To retrieve the public key from a PFX certificate using Powershell, use the following command:

(Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()

To convert the public key to a hex string without hyphens you can use this command:

[System.BitConverter]::ToString((Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()).Replace("-", "")