How to import a certificate using powershell

certificate, powershell, powershell-2.0, x509certificate

Solution

Try running:

certutil -repairstore WSUS "SerialNumber"

Where the "SerialNumber" is the serial number of the imported certificate.

Problem

We are creating a script to automatically create a certificate. We are able to get through all of the stages but when we try to import the certificate into the store we are having issues. The certificate does get installed into the `WSUS -> Certificate Store` but the private key is not associated. If we do it manually it works just fine. The following code represents the section to import the certificate into the store: ``` $cert = new-object system.security.cryptography.x509certificates.x509certificate2 c:\lup.crt $store = New-Object System.Security.Cryptography.X509Certificates.X509Store WSUS, LocalMachine $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) $store.Add($cert) $store.Close() ``` I am still on the learning curve of powershell and the addition of certificates has made this difficult. What am I doing wrong that does not allow the private key to be associated when the certificate is imported to the wsus store? UPDATE So I have updated my code with what `Neossian` has suggested and it does work. However, I noticed it does not delete the private key out of `Enrollment Requests` I can delete this manually but why is it not deleted as it would be if I were to import the certificate manually?

Original source