PowerShell: Generate Self-Signed Certificate and Encode to Base64

Here’s how you can generate a self-signed SSL certificate in PowerShell. Be sure to use an Administrative PowerShell session. After creating the self-signed certificate, you can also export the certificate as a .pfx file and encode the output to a base64 string. This is useful for creating resources like the Application Gateway in Azure via ARM template.

$Cert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName "my.selfsigned.site.biz"
 
$securePwd = ConvertTo-SecureString -String "password!" -Force -AsPlainText
Export-PfxCertificate -Cert "Cert:\LocalMachine\My\$($Cert.Thumbprint)" -FilePath 'c:\temp\MyCert.pfx' -Password $securePwd
 
$fileBytes = Get-Content 'c:\temp\MyCert.pfx' -Encoding Byte
[System.Convert]::ToBase64String($fileBytes) | Out-File 'c:\temp\MyCert.txt'

Hope this helps!

This entry was posted in Azure, PowerShell. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *