Search code examples
powershellssl-certificate

Export-PfxCertificate : Cannot export non-exportable private key


I am attempting to export my self-signed certificate so I can import it to other Servers in my development environment (will use "real" certs for Production), but it throws the following error:

Export-PfxCertificate : Cannot export non-exportable private key

The requirements are that I need to export the cert and "allow the private key to be exported", but am curious what I am missing. My PowerShell is as follows:

$pwd = ConvertTo-SecureString -String ‘1234’ -Force -AsPlainText
$path = 'cert:\localMachine\my\' + '1E7439053EE57AEE6EA0E1F3CDF5DB4234B6731E' 
Export-PfxCertificate -cert $path -FilePath c:\Certificates\cert.pfx -Password $pwd

Solution

  • The problem isn't with the powershell code. The problem is with the certificate.

    When a certificate is first imported or created, the private key must be marked as exportable in order for you to be able to export the private key.

    The error message you have received indicates that the private key is not exportable on the certificate you are trying to use.

    Example Issue