Skip to content

Instantly share code, notes, and snippets.

@Digiover
Created June 7, 2023 08:21
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 Digiover/a2db4125efb069d62a8fedcd23212103 to your computer and use it in GitHub Desktop.
Save Digiover/a2db4125efb069d62a8fedcd23212103 to your computer and use it in GitHub Desktop.
Import a PFX certificate into Windows Server and change its FriendlyName property in PowerShell
# Import a PFX certificate into Windows Server and change its
# FriendlyName property in PowerShell.
#
# Using Get-Credential here ensures the password doesn't get saved
# into PowerShell's history file.
# -- see https://www.saotn.org for more PowerShell goodness :)
#
$CommonName = "CHANGEME"
$CertFileName = "CHANGEME.pfx"
$mypwd = Get-Credential -UserName "${CommonName} SSL certificate" -Message 'Enter password below'
$params = @{
FilePath = "\SSL-certs\${CommonName}\${CertFileName}"
CertStoreLocation = 'Cert:\LocalMachine\My'
Password = $mypwd.Password
}
$cert = Import-PfxCertificate @params
# Update the certificate FriendlyName with the CommonName string and
# year it expires
$cert.FriendlyName = "${CommonName} expires $((Get-Date).AddYears(1)).ToString("yyyy")"
@Digiover
Copy link
Author

Digiover commented Feb 1, 2024

If the last line ($cert.FriendlyName = ...) doesn't work, you can use:

string]$thumbprint = $cert.Thumbprint
(Get-ChildItem -Path Cert:\LocalMachine\My\${thumbprint}).FriendlyName = "${certificatename} 2024"

See https://www.saotn.org/install-ssl-tls-certificates-in-windows-server-using-powershell/ for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment