PoshCode Archive  Artifact Content

Artifact d4824001bd176608934923692c0b83b368fa67af38a2a0c1a3144cf108265c8a:

  • File Search-CertificateStore.ps1 — part of check-in [73b8b2efd9] at 2018-06-10 14:07:27 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1593)

# encoding: ascii
# api: powershell
# title: Search-CertificateStore.
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 4.0
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 6072
# x-archived: 2016-05-24T22:21:54
# x-published: 2016-10-29T15:10:00
#
#
##############################################################################
##
## Search-CertificateStore
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
## updated by Jeremy Wieland - For Powershell v4.0
##############################################################################

<#

.SYNOPSIS

Search the certificate provider for certificates that match the specified
Enhanced Key Usage (EKU.)

.EXAMPLE

Search-CertificateStore "Encrypting File System"

#>

param(
    ## The friendly name of an Enhanced Key Usage
    ## (such as 'Code Signing')
    [Parameter(Mandatory = $true)]
    $EkuName
)

$cert = Get-ChildItem cert:\CurrentUser\My

Set-StrictMode -Off

## Go through every certificate in the current user's "My" store
foreach($cert in $cert)
{
    ## For each of those, go through its extensions
    foreach($extension in $cert.Extensions)
    {
        ## For each extension, go through its Enhanced Key Usages
        foreach($certEku in $extension.EnhancedKeyUsages)
        {
            ## If the friendly name matches, output that certificate
            if($certEku.FriendlyName -eq $EkuName)
            {
                $cert
            }
        }
    }
}