Export PfxCertificate - PowerShell


Overview

The Export-PfxCertificate command exports a Personal Information Exchange (PFX) certificate from a certificate store. PFX certificates are commonly used for secure communication over the internet, such as TLS/SSL.

Syntax

Export-PfxCertificate -CertStoreLocation <CertStoreLocation> -InputFile <InputFile> -OutputPath <OutputPath> -Password <Password>

Options/Flags

  • -CertStoreLocation: Specifies the location of the certificate store from which to export the certificate. Valid values include CurrentUser, LocalMachine, and CA.
  • -InputFile: Specifies the path to the input certificate file.
  • -OutputPath: Specifies the path to the exported PFX certificate file.
  • -Password: Specifies the password to protect the PFX certificate.

Examples

Simple Export:

Exports a certificate from the CurrentUser store and saves it as mycert.pfx.

Export-PfxCertificate -CertStoreLocation CurrentUser -InputFile mycert.cer -OutputPath mycert.pfx -Password "password"

Complex Export:

Exports a certificate from the LocalMachine store, using a non-default password and specifying a specific path for the output file.

Export-PfxCertificate -CertStoreLocation LocalMachine -InputFile mycert.cer -OutputPath c:\certificates\mycert.pfx -Password "complex-password"

Common Issues

  • Incorrect Password: An incorrect password will result in an error. Ensure the password entered matches the password used to protect the certificate.
  • Invalid Certificate File: Verify that the specified input certificate file is valid and contains the desired certificate.
  • Insufficient Permissions: If exporting from the LocalMachine store, elevated privileges may be required. Run PowerShell as an administrator.

Integration

With ConvertFrom-Certificate:

Converts a base-64 encoded certificate to DER format, then exports it as a PFX certificate.

$cert = ConvertFrom-Certificate -Base64Encoded $base64cert
Export-PfxCertificate -InputFile $cert -OutputPath mycert.pfx -Password "password"

With New-SelfSignedCertificate:

Creates a self-signed certificate and exports it as a PFX certificate.

New-SelfSignedCertificate -SubjectName "CN=MySelfSignedCert" -ExportTo Pfx -FilePath mycert.pfx -Password "password"
  • Get-PfxCertificate
  • Import-PfxCertificate
  • Export-Certificate