Generating a Self-Signed Certificate using Powershell

A self-signed certificate it's very easy to create and helps on with 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"
Create Self Signed Cert

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)
Create Self Signed Cert with Expiration Date
Generating Certificate

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
Create Self Signed Cert with Password
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 / Subscribe!

How was the tutorial?

Love Discord?