Convert PFX SSL certificate to base64 in PowerShell and PowerShell Core

less than 1 minute read

Several resource in Azure requires sending the SSL cert data, you can get this by generating it from the SSL PFX file.

Regular powershell

$fileContentBytes = get-content 'your-cert.pfx' -Encoding Byte
[System.Convert]::ToBase64String($fileContentBytes) | Out-File ‘pfx-encoded-bytes.txt’

PowerShell Core

The first -Encoding Byte will cause an error in PowerShell Core, so you will actually neeed to use AsByteStream.

PowerShell Core Error

``` PowerShell Core Set-Content : Cannot bind parameter ‘Encoding’. Cannot convert the “Byte” value of type “System.String” to type “System.Text.Encoding”.


``` PowerShell Core
$Set-Content -Path 'your-cert.pfx' -AsByteStream
[System.Convert]::ToBase64String($fileContentBytes) | Out-File ‘pfx-encoded-bytes.txt’

Updated:

Comments