{"id":2483,"date":"2013-12-04T00:01:00","date_gmt":"2013-12-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/04\/getting-started-with-powershell-the-certificate-provider\/"},"modified":"2013-12-04T00:01:00","modified_gmt":"2013-12-04T00:01:00","slug":"getting-started-with-powershell-the-certificate-provider","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-powershell-the-certificate-provider\/","title":{"rendered":"Getting Started with PowerShell: The Certificate Provider"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Certificate provider.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new Microsoft Press book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-First-Steps\/dp\/0735681007\/ref=la_B001ILFMZ8_1_2_title_1_pap?s=books&amp;ie=UTF8&amp;qid=1383673887&amp;sr=1-2\">Windows PowerShell 3.0 First Steps<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0488.First%20Steps%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0488.First%20Steps%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p>To find information about the Windows PowerShell Certificate provider, use the <strong>Get-Help<\/strong> cmdlet. If you are unsure what topics in Help may be related to certificates, you can use the wildcard character asterisk (*) parameter. This command is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Get-Help *cer*<\/p>\n<p>The Certificate provider gives you the ability to sign scripts, and it allows Windows PowerShell to work with signed and unsigned scripts. It also gives you the ability to search for, copy, move, and delete certificates. With the Certificate provider, you can open the Certificates Microsoft Management Console (MMC) by using the <strong>Invoke-Item<\/strong> cmdlet. The following command illustrates this technique:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Invoke-Item cert:<\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> The Certificate provider does not load by default. The module that contains the Certificate provider, Microsoft.PowerShell.Security, does not automatically import into every session. To use the Cert: drive, use the <strong>Import-Module<\/strong> cmdlet to import the module, or run a command that uses the <strong>Cert:<\/strong> drive, such as a &#8220;<strong>Set-Location Cert:<\/strong>&#8221; command.<\/p>\n<h3>Searching for specific certificates<\/h3>\n<p>To search for specific certificates, you may want to examine the <strong>Subject<\/strong> property. For example, the following command examines the <strong>Subject<\/strong> property of every certificate in the <strong>CurrentUser<\/strong><em> <\/em>store, beginning at the root<em> <\/em>level. It does a recursive search, and returns only the certificates that contain the word <em>test <\/em>in some form in the <strong>Subject<\/strong> property. This command and its associated output are shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\Users\\administrator.IAMMRED&gt; dir Cert:\\CurrentUser -Recurse | ? subject -match<br \/> &#8216;test&#8217;<\/p>\n<p> &nbsp;&nbsp;&nbsp; Directory: Microsoft.PowerShell.Security\\Certificate::CurrentUser\\Root<\/p>\n<p> Thumbprint&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Subject<br \/> &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;-<br \/> 8A334AA8052DD244A647306A76B8178FA215F344&nbsp; CN=Microsoft Testing Root Certificate A&#8230;<br \/> 2BD63D28D7BCD0E251195AEB519243C13142EBC3&nbsp; CN=Microsoft Test Root Authority, OU=Mi&#8230;<\/p>\n<p>Deleting these test<em> <\/em>certificates simply requires piping the results of the previous command to the <strong>Remove-Item<\/strong> cmdlet.<\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> When you perform any operation that may alter system state, it is a good idea to use the <strong>Whatif<\/strong> parameter to prototype the command prior to actually executing it.<\/p>\n<p class=\"Readeraidonly\">The following command uses the <strong>Whatif<\/strong> parameter from <strong>Remove-Item<\/strong> to prototype the command to remove all of the certificates from the <strong>CurrentUser<\/strong> store that contain the word <em>test <\/em>in the <strong>Subject<\/strong><em> <\/em>property. After completion, retrieve the command via the Up arrow and remove the <strong>Whatif<\/strong> switched parameter from the command prior to actual execution. This technique is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\Users\\administrator.IAMMRED&gt; dir Cert:\\CurrentUser -Recurse | ? subject -match<br \/> &#8216;test&#8217; | Remove-Item -WhatIf<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">What if: Performing operation &#8220;Remove certificate&#8221; on Target &#8220;Item: CurrentUser\\Root\\<br \/> 8A334AA8052DD244A647306A76B8178FA215F344 &#8220;.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">What if: Performing operation &#8220;Remove certificate&#8221; on Target &#8220;Item: CurrentUser\\Root\\<br \/> 2BD63D28D7BCD0E251195AEB519243C13142EBC3 &#8220;.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\Users\\administrator.IAMMRED&gt; dir Cert:\\CurrentUser -Recurse | ? subject -match<br \/> &#8216;test&#8217; | Remove-Item<\/p>\n<h3>Finding expiring certificates<\/h3>\n<p>A common task in companies that use certificates is to identify certificates that have expired or are about to expire. By using the Certificate provider, it is simple to identify expired certificates. To do this, use the <strong>NotAfter<\/strong><em> <\/em>property from the certificate objects that are returned from the certificate drives. One approach is to look for certificates that expire prior to a specific date, as shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS Cert:\\&gt; dir .\\\\CurrentUser -Recurse | where notafter -lt &#8220;5\/1\/2012&#8221;<\/p>\n<p class=\"Normalunindented\">A more flexible approach is to use the current date. Therefore, each time the command runs, it retrieves expired certificates. This technique is shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS Cert:\\&gt; dir .\\\\CurrentUser -Recurse | where notafter -lt (Get-Date)<\/p>\n<p class=\"Normalunindented\">One problem with simply using the <strong>Get-ChildItem<\/strong> cmdlet on the <strong>CurrentUser<\/strong><em> <\/em>store is that it returns certificate stores in addition to certificates. To obtain only certificates, you must filter out the <strong>psiscontainer<\/strong><em> <\/em>property.<\/p>\n<p class=\"Normalunindented\">Because you will also need to filter based on date, you can no longer use the simple <strong>Where-Object<\/strong> syntax. The following command retrieves the expiration dates, the thumbprints, and the subjects of all expired certificates. It also creates a table that displays the information. (The command is a single logical command, but it is broken at the pipeline character to permit better display in the book.)<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS Cert:\\&gt; dir .\\\\CurrentUser -Recurse | <br \/> where { !$_.psiscontainer -AND $_.notafter -lt (Get-Date)}&nbsp; | <br \/> ft notafter, thumbprint, subject -AutoSize &ndash;Wrap<\/p>\n<p class=\"Readeraidonly\" style=\"padding-left: 30px\"><strong>Note&nbsp;<\/strong> All versions of Microsoft Windows ship with expired certificates to permit verification of old executables that were signed with those certificates. Do not arbitrarily delete an expired certificate or you could cause serious damage to your system.<\/p>\n<p class=\"Readeraidonly\">If you want to identify certificates that will expire in the next thirty days, you use the dynamic parameter <strong>&ndash;ExpiringInDays<\/strong> from the <strong>Get-ChildItem<\/strong> cmdlet. This dynamic parameter adds to the <strong>Get-ChildItem<\/strong> cmdlet when it is used on the <strong>Cert:<\/strong> drive. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS Cert:\\&gt; Get-ChildItem -Recurse -ExpiringInDays 30<\/p>\n<p>To produce a useful display, select the <strong>Subject<\/strong> and the <strong>NotAfter<\/strong> parameters and sort by the <strong>NotAfter<\/strong> parameter. Then pipe the output to a table that is autosized and wrapped. The command and its output are shown here:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS Cert:\\&gt; gci -ExpiringInDays 30 -r | select subject, notafter | sort notafter | ft<br \/> notafter, subject -a -wr<\/p>\n<p> NotAfter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Subject<br \/> &#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;-<br \/> 2\/12\/2013 6:34:47 PM<br \/> 2\/16\/2013 2:56:37 PM CN=KenMyer@microsoft.com<br \/> 3\/4\/2013 4:42:09 PM&nbsp; CN=Microsoft Corporation, OU=MOPR, O=Microsoft Corporation,<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; L=Redmond, S=Washington, C=US<br \/> 3\/4\/2013 4:42:09 PM&nbsp; CN=Microsoft Corporation, OU=MOPR, O=Microsoft Corporation,<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; L=Redmond, S=Washington, C=US<\/p>\n<p>That is all there is to working with the Certificate provider. Join me tomorrow when I will have another excerpt from my Microsoft Press book, Windows PowerShell 3.0 First Steps.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Certificate provider. Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new Microsoft Press book, Windows PowerShell 3.0 First Steps. To find information about the Windows PowerShell Certificate provider, use the Get-Help cmdlet. If you are unsure what [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-2483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Certificate provider. Microsoft Scripting Guy, Ed Wilson, is here. Today I have an excerpt from my new Microsoft Press book, Windows PowerShell 3.0 First Steps. To find information about the Windows PowerShell Certificate provider, use the Get-Help cmdlet. If you are unsure what [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2483\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}