g.pickardou
g.pickardou

Reputation: 35853

How to find and delete previously created certificates based on their dns name using powershell?

I can create a self signed certificate with PowerShell:

$cert = New-SelfSignedCertificate –DnsName www.test.com -CertStoreLocation “cert:\LocalMachine\My”

Because this is an automated test scenario, I must allow multiple runs. Next time I (which is a completely separate session) would like to find all certificates with dns name www.test.com, and completely wipe them. It could be more than one, of course with different thumbprint, etc.

Unfortunatelly I can not find any PowerShell command removes certificates. I suppose I should query based on dns, then in a loop remove...

Question

Can this task solved at all entirely in Powershell? (remove all certificates with given dns name)

Upvotes: 0

Views: 1256

Answers (2)

TheMadTechnician
TheMadTechnician

Reputation: 36297

If you have PowerShell v3 or greater this becomes very simple, as PS3 introduced the -DnsName parameter to the Certificate provider options of Get-ChildItem. This will find and delete anything with the DNSName of www.test.com:

Get-ChildItem Cert:\ -Recurse -DnsName 'www.test.com' | Remove-Item

It even supports wildcards, so if you have several certificates that you need to clean up with similar names such as 'mailproxy.test.com', 'www.test.com', and 'ftp.test.com' you could run this:

Get-ChildItem Cert:\ -Recurse -DnsName '*.test.com' | Remove-Item

Upvotes: 5

bluuf
bluuf

Reputation: 1001

Simple oneliner for this :

Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object {$_.DnsNameList -contains 'www.domain.org'} | Remove-Item

Remove-Item is the command used to remove a certificate.

Upvotes: 1

Related Questions