Importing Certificates from PFX Files

by Feb 11, 2014

You can use Get-PfxCertificate to load digital certificates from PFX files, and then use the certificate to sign script files, for example:

$pfxpath = 'C:\PathToPfxFile\testcert.pfx'
$cert = Get-PfxCertificate -FilePath $pfxpath 
$cert

Get-ChildItem -Path c:\myscripts -Filter *.ps1 | Set-AuthenticodeSignature -Certificate $cert

However, Get-PfxCertificate will interactively ask for the password that you specified when you originally exported the certificate to the PFX file.

To import a certificate unattended, try this code instead:

$pfxpath = 'C:\PathToPfxFile\testcert.pfx'
$password = 'topsecret'

Add-Type -AssemblyName System.Security
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($pfxpath, $password, 'Exportable')
$cert 

Twitter This Tip! ReTweet this Tip!