To convert a certificate that is in
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 a certificate to Azure to secure communication to backend instance. Since Microsoft Azure provides a rich API to work with. I was able to make a patch request and push a certificate to Azure.
In this tutorial, I will show you how to convert a certificate from .pfx to base64.
Open PowerShell as an administrator.
Now that we have a 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 the PowerShell console, you will see random numbers. In order to convert to base64 format, we will use system namespace from Windows system.
To convert a certificate in base64, execute the 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.