How to generate a code signing certificate and sign PowerShell .ps1 script
This guide will show you how to issue a code signing certificate from your internal Certificate Authority, and how to use it to sign your code.
Topics covered in this article:
Trying to sign code with no certificate available
If you would like to sign your own files, and this is done on a machine where no code signing certificate is present, you will get an error.
- If you are using SignTool to automatically select the best signing certificate, the verbose output will say:
SignTool Error: No certificates were found that met all the given criteria.
- If you are using the Set-AuthenticodeSignature PowerShell cmdlet to do the signing you get the error message:
Set-AuthenticodeSignature : Cannot bind argument to parameter ‘Certificate’ because it is null.
How to get a code signing certificate
One option would be to purchase a code signing certificate online from authorities such as DigiCert.
Another one would be to issue one from your internal Certificate Authority, which you can do by following these steps:
Create and issue a signing certificate
- Open Certification Authority (certsrv.msc) on a machine where you have installed the certification authority.
- Expand the name of the Certification Authority, then right-click on Certificate Templates and choose Manage
- Right-Click on Code Signing under the Template Display Name column and choose Duplicate template.
- On the properties of the new template, click on the General tab and give it the name you want. Also, choose the validity period. Save change with Apply.
- Go on the Request Handling tab, and make sure Allow private key to be exported is enabled.
- On the Subject Name tab, set the Subject name format to Common Name.
- On the Extensions tab, make sure that the description of Key Usage contains Digital Signature.
- On the Security tab, ensure that “Authenticated Users” have Read and Enroll permissions.
- You can now click OK and then close the Certificate Templates Console.
- Back to Certification Authority, right click Certificate Templates, choose New and then select Certificate Template to Issue.
- From the Enable Certificate Templates list, select your certificate template and confirm with OK.
Requesting a certificate on a machine to use it for code signing
- On a domain joined machine, open mmc.exe.
- Click File and then Add/Remove Snap-in.
- Select Certificates and then click on Add.
- In the dialog box which appears, select My user account, then confirm with Finish.
- In the console, expand Certificates – Current User, then expand Personal.
- Right-click Certificates, then go to All Tasks and select Request New Certificate.
- In the Certificate Enrollment window, click Next until you see a list of certificates to request. The issues certificate should appear in this list. Select it and confirm with Enroll.
- The certificate should now be enrolled on that device and can be used for code signing.
How to sign your code
Example 1: SignTool
In this example, we used the following arguments:
- sign = Digitally signs files. Digital signatures protect files from tampering, and enable users to verify the signer based on a signing certificate.
- /a = Automatically selects the best signing certificate. Sign Tool will find all valid certificates that satisfy all specified conditions and select the one that is valid for the longest time. In our case, it automatically selected the code signing certificate we enrolled.
- /v = Displays verbose output
- /fd = specifies the digest algorithm. In our case, we went with SHA256.
Example 2: PowerShell cmdlet Set-AuthenticodeSignature
In this example, on the server where the Patch My PC Publishing service is installed, from an elevated PowerShell ISE instance:
- On line 1, we get the code signing certificate we enrolled.
- On line 3, we used the Set-AuthenticodeSignature cmdlet to sign our C:\test.ps1 file using the WSUS code signing certificate.