Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to edit a piece of found code that will allow me to delete personal certificates that are NOT the primary users certificates. For instance, my desktop folks as well as my HR folks constantly work with customers and acquire daily a large sum of other users digital certificates which can be found with Certificate Manager under Certificates - Current User\Personal\Certificates. I want to target any certificate not of a specific group, but do not know enough to correctly modify the command (posted below)

What I have tried:

Get-ChildItem Cert:\CurrentUser\My |
Where{$_.Issuer -match 'Communication Server'} |
Remove-Item -WhatIf

Is the command line I found elsewhere, I can see on the second line the command is looking for certificates that are issued by 'Communication Server'. What I want to do is exclude XYZ certificates, and flush all others.
Posted
Updated 17-Apr-21 2:51am
Comments
Richard MacCutchan 17-Apr-21 4:13am    
Run the first line only (without the pipe), and that will give you the full list. You can then see what you need to filter to the delete process.

1 solution

Hello Richard,
  Thank you for your response, I ran "Get-ChildItem Cert:\CurrentUser\My" and it produced the list of certificates on a test machine.  The second line of "Where{$_.Issuer -match 'Communication Server'}" is what I need to cater to my situation.  I need the command line to not touch XYZ specific certificates, but delete all others.  Do you know what replaces .Issuer (displayed as "Issued By" in Cert Manager) to "Issued To"?  
 
Share this answer
 
Comments
Richard MacCutchan 17-Apr-21 11:39am    
Please do not post questions or comments as solutions, but use the Reply button above the posted message.

However, in answer to your question, you will need to filter all the certificate entries that do not contain your keyword(s). You can use the -NotMatch option of the Where clause.
Christopher Kratzner 18-Apr-21 8:10am    
Thank you for correcting me Richard, I should have paid better attention. Is it possible for me to input multiple lines of "Where{$_.IssuedTo -match '123456'} to identify more than one certificate or is there a way to do so in the same command line?
Richard MacCutchan 18-Apr-21 8:12am    
As with everything Powershell there are lots of options. Take a look at Where-Object (Microsoft.PowerShell.Core) - PowerShell | Microsoft Docs[^].
Christopher Kratzner 19-Apr-21 13:19pm    
Thank you for the reference Richard, please bear with me, my experience with this sort of material is light and rudimentary at best. If I am interpreting the reference items correctly, should I be inputting into the command should be something along the lines of:

“Where{$_.IssuedTo -NotMatch ‘cert1’ -and ‘cert2’ -and ‘cert3’}

(‘cert#’ is just hypothetical)
Richard MacCutchan 20-Apr-21 3:28am    
You canjnot have multiple and expressions like that. The and operator is a binary operator and requires two operands in every place thus:
Where{$_.IssuedTo -NotMatch ‘cert1’ -and $_.IssuedTo -NotMatch ‘cert2’ -and $_.IssuedTo -NotMatch ‘cert3’}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900