Nothing and Everything

2011.08.17

Powershell script to delete a certificate

Filed under: Programming — kevenker @ 2:21 pm

This is just a quick little script to delete a certificate using powershell. It’s not as flexible as one might want it to be but should get you started. Plus, it could be optimized a bit, but it gets the job done! :)

You’ll notice the line:

new-object System.Security.Cryptography.X509Certificates.X509Store "My","CurrentUser"

The “My” is the StoreName (AddressBook, AuthRoot, CertificateAuthority, Disallowed, My, Root, TrustedPeople, TrustedPublisher). See http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storename.aspx for more info.

“CurrentUser” is the StoreLocation with “CurrentUser” or “LocalMachine” the only two options at present. See http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.storelocation.aspx for more info.

 

param
(
 [parameter(Mandatory=$true)][string]$certPattern
)
write-host "`nCertificate search pattern = '$certPattern'"
$store = new-object System.Security.Cryptography.X509Certificates.X509Store "My","CurrentUser"
$store.Open("ReadWrite")
$certs = $store.Certificates
foreach ($cert in $certs)
{
    if ($cert -like $certPattern)
    {
        write-host "Deleting: "$cert.Thumbprint $cert.Subject
        $store.Remove($cert)
    }
}
$store.Close()
Advertisement

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.