Importing and Installing Certificate

by Nov 4, 2014

All PowerShell versions

To programmatically load a certificate from a file and install it in a specific location inside the certificate store, have a look at this script:

$pfxpath = 'C:\temp\test.pfx'
$password = 'test'
[System.Security.Cryptography.X509Certificates.StoreLocation]$Store = 'CurrentUser'
$StoreName = 'root'

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

$Store = New-Object system.security.cryptography.X509Certificates.x509Store($StoreName, $StoreLocation)
$Store.Open('ReadWrite')
$Store.Add($certificate)
$Store.Close()

You can configure the script and specify the location and password of the certificate file to import. You can also specify the store location (CurrentUser or LocalMachine), and the container to put the certificate into (for example “root” for trustworthy root certificates, or “my” for personal certificates).

Twitter This Tip! ReTweet this Tip!