How to Create and Export SelfSigned Certificate from Powershell Script



In many cases we want to create a self-signed certificate from PowerShell scripts 

Create Certificate and Export Certificate by setting password

  1. Get the date details and save in variable $todaydt = Get-Date
  2. Set number of years of certificate expiry $3years = $todaydt.AddYears(3)
  3. Now run this command to create certificate without expiry date “New-SelfSignedCertificate -DnsName ADFSQAWIN19.ADHIDQA.com  -CertStoreLocation cert:\LocalMachine\My
  4. Run this command to create certificate with expiry date “New-SelfSignedCertificate -dnsname ADFSQAWIN19.ADHIDQA.com -notafter $3years -CertStoreLocation cert:\LocalMachine\My”
  5. Save password as plain text “$CertPassword = ConvertTo-SecureString -String ‘<my password>’ -Force –AsPlainText”
  6. Export with this command “Export-PfxCertificate -Cert cert:\LocalMachine\My\<Certificate ID which you will get after create certificate> -FilePath C:\test.pfx -Password $CertPassword”

Comments