Skip to content

Instantly share code, notes, and snippets.

@dentolos19
Created February 16, 2023 04:24
Show Gist options
  • Save dentolos19/803820c8b305224db8167e6d5b158691 to your computer and use it in GitHub Desktop.
Save dentolos19/803820c8b305224db8167e6d5b158691 to your computer and use it in GitHub Desktop.
Set-Location $PSScriptRoot
Write-Host "PFX Certificate Creator"
Write-Host ""
Write-Host "Create a PFX certificate for use with MSIX Package Signing."
Write-Host ""
$subjectName = Read-Host "Subject Name"
$friendlyName = Read-Host "Friendly Name"
$certificateKey = Read-Host "Certificate Key" -AsSecureString
$certificateStore = "Cert:\CurrentUser\My"
New-SelfSignedCertificate `
-Type Custom `
-Subject "CN=$subjectName" `
-KeyUsage DigitalSignature `
-FriendlyName $friendlyName `
-CertStoreLocation $certificateStore `
-TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}") | `
Out-Null
$certificate = Get-ChildItem $certificateStore | `
Where-Object { $_.Subject -eq "CN=$subjectName" -and $_.FriendlyName -eq $friendlyName } | `
Select-Object -First 1
Export-PfxCertificate -Cert $certificate -FilePath "$friendlyName.pfx" -Password $certificateKey | Out-Null
$bytes = [System.IO.File]::ReadAllBytes("$PSScriptRoot\$friendlyName.pfx")
$text = [System.Convert]::ToBase64String($bytes)
[System.IO.File]::WriteAllText("$PSScriptRoot\$friendlyName.pfx.txt", $text)
Get-ChildItem "$certificateStore\$($certificate.Thumbprint)" | Remove-Item
Write-Host ""
Write-Host "PFX Certificate created successfully!"
Write-Host "Saved To: $PSScriptRoot\$friendlyName.pfx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment