Created
February 3, 2022 16:46
-
-
Save PhilMurwin/f9d731b5bde8489cadc0778925899c2e to your computer and use it in GitHub Desktop.
Generate Self Signed Cert using powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#GenerateSelfSignedCert_2022.ps1 | |
# Information from: https://www.tutorialspoint.com/how-to-create-a-self-signed-certificate-using-powershell | |
# Additional/Similar information from: https://petri.com/create-self-signed-certificate-using-powershell | |
# Get Current Directory | |
$curDir = Get-Location | |
#Write Header | |
Write-Host "`n WARNING: This script is provided AS-IS with no warranties and confers no rights." -ForegroundColor Yellow | |
Write-Host "`n This script will generate a self-signed certificate with private key" | |
Write-Host " in the Local Computer Personal certificate store and saved in the current directory." | |
Write-Host "`nIt will be set to expire in 10 years." | |
# Get Subject for cert | |
$subject = Read-Host "`nEnter Certificate Subject (e.g. Site Url, or simply name for cert" | |
# Check that a Subject was entered | |
if (!$subject) | |
{ | |
Write-Host "`nSubject cannot be empty!" -ForegroundColor Red | |
Exit | |
} | |
# Get password for cert | |
$password = Read-Host "`nEnter a certificate password" | |
# Check that a password was entered | |
if (!$password) | |
{ | |
Write-Host "`nPassword cannot be empty!" -ForegroundColor Red | |
Exit | |
} | |
# Get an Expiration date 10 years in the future to use for the cert | |
$expDate = (Get-Date).AddDays(3650).ToString("MM/dd/yyyy") | |
# Generate the cert into the local keystore | |
$cert = New-SelfSignedCertificate -Subject $subject -KeyLocation $curDir -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -HashAlgorithm SHA256 -KeyLength 2048 -NotAfter $expDate | |
# Prepare to save the cert to the current directory | |
$securePassword = ConvertTo-SecureString -String $password -Force -AsPlainText | |
$path = "cert:\LocalMachine\my\" + $cert.thumbprint | |
# Save the cert to the current directory | |
Export-PfxCertificate -Cert $path -FilePath "$curDir\$subject.pfx" -ChainOption EndEntityCertOnly -Password $securePassword | |
Export-Certificate -Cert $path -FilePath "$curDir\$subject.cer" | |
CertUtil -encode "$subject.cer" "$subject.b64.cer" | |
# The Script has completed. | |
Write-Host "`n Finished!`n" -ForegroundColor Green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment