Import .cer certificate from Windows command line

certificatecommand-line-interface

Tired of wading throug MMC jungles, I need a way to import a given .cer file (which contains just a public key) into machine-wide certificate store (don't know how is it called in English since I'm using a localized version of Windows) into "Personal Certificates" folder. Both cmd.exe or PowerShell versions will be fine.

Best Answer

Powershell solution, using System.Security.Cryptograpy.X509Certificates:

$filename = "MyFileName.cer"

[Reflection.Assembly]::Load("System.Security, Version=2.0.0.0, Culture=Neutral, PublicKeyToken=b03f5f7f11d50a3a")

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($filename)

$store = New-Object System.Security.Cryptography.X509Certificates.X509Store([System.Security.Cryptography.X509Certificates.StoreName]::My,[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)

$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite);

$store.Add($cert);

Sorry for all the full namespace specifications, but there's no "using" directive in PowerShell.