Skip to content

Instantly share code, notes, and snippets.

@garima2510
Last active January 18, 2022 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garima2510/862b20e2d88609e552ddf405a6b2c3a9 to your computer and use it in GitHub Desktop.
Save garima2510/862b20e2d88609e552ddf405a6b2c3a9 to your computer and use it in GitHub Desktop.
$ErrorActionPreference = "Stop"
#Name of pfx file that needs to be password protected with extension
$pfxFileName = Read-Host -Prompt 'Input pfx file name'
if($pfxFileName) {
#Replace .\ with actual path where .pfx is stored. Do not include file name
$pfxFilePath = (Get-Item -Path ".\").FullName
$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$certCollection.Import("$pfxFilePath\$pfxFileName", $null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)
$password = Read-Host -Prompt 'Enter password for file'
$protectedCertificateBytes = $certCollection.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12, $password)
# New pfx will be generated at same location where existing pfx is with the following name PW-protect-{same}
[System.IO.File]::WriteAllBytes("$pfxFilePath\PW-protect-$pfxFileName", $protectedCertificateBytes)
Write-Host "Operation Completed"
}
#Ref: https://www.powershellbros.com/export-keyvault-certificates-azure/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment