{"id":458,"date":"2008-07-02T02:00:07","date_gmt":"2008-07-02T10:00:07","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/02\/sign-powershell-scripts\/"},"modified":"2008-06-30T07:15:16","modified_gmt":"2008-06-30T15:15:16","slug":"sign-powershell-scripts","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/02\/sign-powershell-scripts\/","title":{"rendered":"Sign PowerShell Scripts"},"content":{"rendered":"<p>If you&#8217;ve been to any of my PowerShell sessions or classes you know I often recommend using an AllSigned execution policy for your production servers. Assuming you&#8217;ve gone ahead and installed a code signing certificate on your desktop, how do you go about signing all your scripts. Here&#8217;s how you might sign all your scripts in a given directory.<\/p>\n<p><code>#assumes one code signing cert<br \/>\nPS C:\\&gt; $cert=Get-ChildItem -FilePath cert:\\CurrentUser\\my -CodeSigningCert<br \/>\nPS C:\\&gt; New-Alias sign Set-AuthenticodeSignature<br \/>\nPS C:\\&gt; dir c:\\scripts\\*.ps1 | foreach {sign $_ $cert}<\/code><\/p>\n<p>\nIf you had a text list with a full path and file to each script you could do something like this:<\/p>\n<p><code>PS C:\\&gt; get-content c:\\scripts.txt | foreach {sign $_ $cert}<\/code><\/p>\n<p>If you have PrimalScript, you can configure it to sign scripts automatically everytime you save a PowerShell file. Again, assuming you only have a single code signing certificate installed, go to to Tools &#8211; Options and navigate to ScriptSettings &#8211; Security. In the PowerShell Security section you should see your execution policy. Leave the certificate field blank and check the box for &quot;Automatically sign scripts when saving&quot;<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/06\/codesigning.png\"><img loading=\"lazy\" decoding=\"async\" height=\"189\" border=\"5\" align=\"left\" width=\"244\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/06\/codesigning-thumb.png\" alt=\"codesigning\" style=\"border-width: 0px;\" \/><\/a><\/p>\n<p>Now whenever you save a PowerShell script it will automatically be signed. If you prefer, you can leave the box unchecked and manually sign your scripts from the Script menu.<\/p>\n<p>Currently Primalscript will use the first code signing certificate it finds. We&#8217;re still trying to find a good mechanism for allowing you to choose a specific code signing certificate. Right now it appears the best solution if you have multiple certificates is to export your certificate to a PFX file and then use the browse button next to the certificate field to load the file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve been to any of my PowerShell sessions or classes you know I often recommend using an AllSigned execution policy for your production servers. Assuming you&#8217;ve gone ahead and installed a code signing certificate on your desktop, how do you go about signing all your scripts. Here&#8217;s how you might sign all your scripts in a given directory.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[201,202,28,57],"class_list":["post-458","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-authenticodesignature","tag-executionpolicy","tag-powershell","tag-scripting"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=458"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/458\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}