Convert certificate from pfx to base64 with PowerShell

Convert certificate from pfx to base64 with PowerShell

To convert certificate that is in .pfx to base64 format in PowerShell, you can use .NET namespace available in PowerShell to convert. I had a scenario where I was required to use base64 encoding to upload certificate to Azure to secure communication to backend instance. Since Microsoft Azure provides rich API to work with. I was able to make a patch request and push certificate to Azure.

In this tutorial, I will show you how to convert certificate from .pfx to base64.

Open PowerShell as an administrator.

Now that we have PowerShell console opened. Let's first load the content into a variable.

$pfx_cert = get-content 'c:\certificate.pfx' -Encoding Byte

The above command will load the content with byte encoding. If you print $pfx_cert in PowerShell console, you will see random numbers. In order to convert to base64 format, we will use system namespace from windows system.

To convert certificate in base64, execute below command.

$base64 = [System.Convert]::ToBase64String($pfx_cert)

The above command will save it in $base64 variable, and now you can use this variable or output it to a file depending on your scenario.