r/PowerShell • u/QuickBooker30932 • Nov 27 '25
Trouble with self-signed security certificate
I'm having trouble with my first self-signed certificate. I followed these steps to create it:
# Create a certificate$selfsigncert = New-SelfSignedCertificate -Subject "CN=PowerShell Code Signing" -KeyAlgorithm RSA -KeyLength 2048 -Type CodeSigningCert -CertStoreLocation Cert:\LocalMachine\My
# Move the root cert into Trusted Root CAsMove-Item "Cert:\LocalMachine\My\$($selfsigncert.Thumbprint)" Cert:\LocalMachine\Root
# Obtain a reference to the code signing cert in Trusted Root$selfsignrootcert = "Cert:\LocalMachine\Root\$($selfsigncert.Thumbprint)"
But signing the script doesn't seem to work. I entered this:
Set-AuthenticodeSignature .\ScriptName.ps1 $selfsignrootcert
And I get this error:
Set-AuthenticodeSignature: Cannot bind parameter 'Certificate'. Cannot convert value "Cert:\LocalMachine\Root\[omitted]" to type "System.Security.Cryptography.X509Certificates.X509Certificate2". Error: "The filename, directory name, or volume label syntax is incorrect."
I've tried using the complete script path in quotes but get the same error.
9
u/BlackV Nov 27 '25 edited Nov 27 '25
Edit... Odd formatting, I hate the app
1
u/QuickBooker30932 Nov 27 '25
I'm afraid all these suggestions and questions are over my head. I copied the commands in my post from somewhere else. Could someone walk me through what I should have done?
1
u/toni_z01 Nov 27 '25
Quite simple - u need to provide the certificate instead of the path.
Change this: $selfsignrootcert = "Cert:\LocalMachine\Root\$($selfsigncert.Thumbprint)"to: $selfsignrootcert = get-item "Cert:\LocalMachine\my\$($selfsigncert.Thumbprint)"3
u/y_Sensei Nov 27 '25
Installing certificates in a non-private certificate store (ie one that's not
...\My) requires administrative permissions on the machine where you do that, for obvious reasons (security).Additionally, the 'Move-Item' cmdlet doesn't support moving certificates to a different certificate store - see here.
1
u/Nu11u5 Nov 27 '25
Does the cert still have the private key after moving it to the root store?
1
u/AusPower85 Nov 27 '25
You only want the cert you created in cert:\locamachine\my You don’t want the ones you created in trusted installers or root. They are the chain behind the cert.
General steps:
Create self signed cert in local machine/my
Import it into trusted installers and root
Get-childitem to get the cert object from localmachine/my
Use the cert to set the authentication signature as you’ve got in Your last step
1
u/purplemonkeymad Nov 27 '25
You still need the cert in your personal store so that you can sign the cert. So you want to use Copy-Item instead. then use the cert in the personal store to sign it ie:
or if it's a different session you can re-populate your variable:
or
for a GUI picker.
2
u/[deleted] Nov 27 '25 edited Nov 27 '25
[removed] — view removed comment