Create a Self-Signed Digital Certificate in PowerShell

#How to run the PowerShell scripts

#SignLib.dll must be placed on a folder and the path must be added on the .ps1 script
#$DllPath = 'd:\SignLib.dll'
#[System.Reflection.Assembly]::LoadFrom($DllPath)

#run the script file from command line, as below:
#powershell -executionPolicy bypass -file d:\YOUR-POWERSHELL-SCRIPT.ps1

#Create a PFX Certificate

$DllPath = 'd:\SignLib.dll'
[System.Reflection.Assembly]::LoadFrom($DllPath)

#create a PFX digital certificate
$generator = new-object -typeName SignLib.Certificates.X509CertificateGenerator("")
$pFXFilePassword = "pfx-cert-password"

#set the certificate subject
$generator.Subject = "CN=User Certificate, O=Organization, C=EU"

#add the digital certificate extensions
$generator.Extensions.AddKeyUsage(
[SignLib.Certificates.CertificateKeyUsage]::DigitalSignature)
$generator.Extensions.AddEnhancedKeyUsage(
[SignLib.Certificates.CertificateEnhancedKeyUsage]::DocumentSigning)

#set the key size and algorithm
$generator.KeySize = [SignLib.Certificates.KeySize]::KeySize2048Bit;
$generator.SignatureAlgorithm = [SignLib.Certificates.SignatureAlgorithm]::SHA256WithRSA;

echo "Create the certificate..."
$certificate = $generator.GenerateCertificate($pFXFilePassword)

#save the digital certificate
[System.IO.File]::WriteAllBytes("d:\pfxcertificate.pfx", $certificate);

See also: