5

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.

Anton Gogolev
  • 1,572
  • 3
  • 16
  • 22

3 Answers3

4

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.

Massimo
  • 68,714
  • 56
  • 196
  • 319
1

I don't know how to import it from the command line, but you don't need MMCs in order to install a certificate, double-clicking on it is just enough.

Massimo
  • 68,714
  • 56
  • 196
  • 319
0

This is wrong. See Massimo's post with the powershell reflection assembly.


Uh ... the powershell way is to open the MMC. Technet Link

This command opens the Certificates MMC snap-in to manage the specified certificate.

invoke item cert:\CurrentUser\my\6B8223358119BB08840DEE50FD8AF9EA776CE66B
Joseph Kern
  • 9,809
  • 3
  • 31
  • 55
  • How does this relate to importing a .cer file, exactly? – Massimo Oct 14 '09 at 14:15
  • This is to say, you can't import a cert from powershell, you need to use the mmc. The mmc can be invoked from powershell. I've been down-voted twice, but no one has posted anything to the contrary. – Joseph Kern Oct 14 '09 at 17:02
  • You've been downvoted because you posted something quite unrelated to the question asked... – Massimo Oct 15 '09 at 08:23
  • 1
    BTW, your answer is actually incorrect, because in PowerShell you have access to the full .NET framework, and System.Security.Cryptograpy.X509Certificates has all you need. – Massimo Oct 15 '09 at 09:24
  • 1
    Lol, fair enough. I see you've posted the REAL answer. Thanks! – Joseph Kern Oct 15 '09 at 11:26