(PowerShell) Import a Certificate (.cer file) into a Windows Certificate Store
Demonstrates how to import a certificate (without private key) into a Windows certificate store.
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$cert = New-Object Chilkat.Cert
$success = $cert.LoadFromFile("qa_data/certs/example.cer")
if ($success -eq $false) {
$($cert.LastErrorText)
exit
}
$certStoreCU = New-Object Chilkat.CertStore
$readOnlyFlag = $false
# "CurrentUser" and "My" are the exact keywords to select your user account's certificate store.
$success = $certStoreCU.OpenWindowsStore("CurrentUser","My",$readOnlyFlag)
if ($success -eq $false) {
$("Failed to open the CurrentUser/My certificate store for read/write.")
exit
}
# Import the certificate into the CurrentUser/My certificate store.
$success = $certStoreCU.AddCertificate($cert)
if ($success -eq $false) {
$($certStoreCU.LastErrorText)
exit
}
$("Imported " + $cert.SubjectCN)
$("Success.")
|