Networknet archive Blog

PowerShell: Export and import an SSL certificate

Originally published 1 comment

Several years ago, I used certutil.exe in a script run through SCCM to import a certificate.

More recently, while setting up ADFS and WAP servers, I needed to export and import a certificate with its private key many times. Automating the process helped keep the servers and services aligned on the same certificate. I used remote PowerShell sessions to speed up the work.

Export the certificate

PowerShell4 lines
1
2
3
4
Enter-PSSession -ComputerName ADFS-01
$thumprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match "CN=ssl.networknet.nl"}).Thumbprint
$pwd = ConvertTo-SecureString -String "password" -Force -AsPlainText
Get-ChildItem -Path Cert:\LocalMachine\My\$thumprint | Export-PfxCertificate -FilePath C:\ssl.networknet.nl.pfx -Password $pwd

Import the certificate

PowerShell3 lines
1
2
3
Enter-PSSession -ComputerName ADFS-02
$pwd = ConvertTo-SecureString -String "password" -Force -AsPlainText
Import-PfxCertificate -FilePath C:\adfs.networknet.nl.pfx Cert:\LocalMachine\My -Password $pwd

The current script does not copy the exported PFX file to the remote server's C:\ volume. That file still needs to be copied, although this step could also be automated with PowerShell.

1 comment

Comments from the original WordPress site, kept read-only.