PS_CreateCerficate.ps1

#https://blogs.u2u.be/u2u/post/creating-a-self-signed-code-signing-certificate-from-powershell

#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
$Subject = "CN=PA1REG PSDxExpeditions Code Signing" 
Get-ChildItem -Path Cert:\CurrentUser\Root | ? Subject -EQ "$Subject" | Remove-Item

$cert = New-SelfSignedCertificate -CertStoreLocation cert:\currentuser\my `
-Subject "$Subject" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" `
-KeyExportPolicy Exportable `
-KeyUsage DigitalSignature `
-Type CodeSigningCert `
-NotAfter (Get-Date).AddMonths(10*12)


Get-ChildItem -Path Cert:\CurrentUser\My | ? Subject -EQ "$Subject"

Move-Item -Path $cert.PSPath -Destination "Cert:\CurrentUser\Root"

Set-AuthenticodeSignature -FilePath .\PSDxExpeditions.psm1 -Certificate $cert

#Set-ExecutionPolicy -ExecutionPolicy AllSigned


#$ReturnCert = Get-ChildItem -Path Cert:\CurrentUser\Root | ? Subject -EQ "$Subject"
#$CertPassword = ConvertTo-SecureString -String 'pa1reg' -Force �AsPlainText
$CertPassword = Read-Host -Prompt "Enter the certificate password, type ""pa1reg"" here"  -AsSecureString

Export-PfxCertificate -Cert "Cert:\CurrentUser\Root\$($Cert.Thumbprint)" -FilePath .\PA1REG_DxExpeditions.pfx -Password $CertPassword