Generating a Self Signed Certificate using Powershell

rmaurodev

Ricardo

Posted on September 22, 2020

Generating a Self Signed Certificate using Powershell

A self-signed certificate it's very easy to create and helps with our local development and testing.

With a Single Line of PowerShell code, we create a certificate.

First, open the PowerShell as Administrator and run the following command:

New-SelfSignedCertificate `
   –DnsName <DNS-Name> `
   -CertStoreLocation "cert:\LocalMachine\My"
Enter fullscreen mode Exit fullscreen mode

The default expiration is 1 year. If you want a custom expiration date use option -NotAfter.

New-SelfSignedCertificate `
   –DnsName <DNS-Name> `
   -CertStoreLocation "cert:\LocalMachine\My" `
   -NotAfter [System.DateTime]::AddYears(3)
Enter fullscreen mode Exit fullscreen mode

PowerShell result

That is it. Done!! The certificate was created and stored in our Certificate Store of Windows.

Note the parameter "CertStoreLocation", this is where the cert will be stored. cert:\LocalMachine means Local Machine Cert store.

Now, let's export it as a .pfxfile into a local directory.
In the same Powershell window run the following commands.

#create a password for our cert
$pwd = ConvertTo-SecureString -String "SOME-PASSWORD" -Force -AsPlainText

#finds the certificate in our local store
$cert = Get-ChildItem -Path cert:\LocalMachine\my | where Subject -eq "CN=rmauro.dev" 

#exports the certificate to temp directory
Export-PfxCertificate -FilePath c:\temp\rmauro.dev.pfx -Password $pwd -Cert $cert
Enter fullscreen mode Exit fullscreen mode

Export result

In my scenario, the cert name is rmauro.dev. Change it to yours.
Check the directory temp to find the certificate - rmauro.dev.pfx.

Leave a comment if you like it or dislike.

Also check out my blog https://rmauro.dev.

💖 💪 🙅 🚩
rmaurodev
Ricardo

Posted on September 22, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related