Search code examples
powershellcertificate

New-SelfSignedCertificate -CertStoreLocation cannot find path


Using the New-SelfSignedCertificate cmdlet, I want to specify the location on my harddrive as C:\Development\My Project. This command:

New-SelfSignedCertificate -CertStoreLocation "cert:\LocalMachine\Development\My Project" 

gives this error:

Cannot find path 'Cert:\LocalMachine\Development\My Project' because it does not exist.

How do I do this?

$PSVersionTable.PSVersion
Major  Minor  Build  Revision
-----  -----  -----  --------
5      0      10586  962

Solution

  • The path that you specify for New-SelfSignedCertificate -CertStoreLocation is a certificate store, not a file path. What you will most likely want to do is specify cert:\LocalMachine\my which will create the certificate in your personal store, and then export that certificate to a file on the hard drive if you need it in file form. Something like this should work for that:

    $notAfter = [datetime]::Today.AddYears(2)
    $thumb = (New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -DnsName $env:USERDNSDOMAIN -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $notAfter).Thumbprint
    $pwd = 'SuperS3cret!'
    $SSpwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
    Export-PfxCertificate -cert "cert:\localmachine\my\$thumb" -FilePath "C:\Development\My Project\MyDevCert.pfx" -Password $SSpwd