PowerShell commands to delete personal certificates

Chris Krat 1 Reputation point
2021-04-16T17:12:13.35+00:00

I came across a great post from the old TechNet and ran into an issue that is similar to it (listed below)

https://social.technet.microsoft.com/Forums/en-US/2e472d2e-98d3-4621-bfb2-be18733ad412/script-to-remove-a-local-certificate-possible?forum=winserverpowershell

I am trying to use PowerShell to delete personal certificates other than the ones belonging to the primary user of the computer. Our HR folks deal with this constantly and am looking to provide them a simple script of sorts to simply double-click and wash away all the other user certificates not their own. The one I saw targeted specifically certificates within certificate manager that were issued by however. What code or commands do I use to target deletion of certificates Issued To?

Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Anonymous
    2021-04-19T04:02:49.677+00:00

    Hi,

    You can filter with the "Subject" property like this

    $user="test user"  
    Get-ChildItem Cert:\CurrentUser\My | Where-Object {$_.Subject -match $user} | Remove-Item  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

  2. Chris Krat 1 Reputation point
    2021-04-19T16:54:37.313+00:00

    Hello IanXue-MSFT,
    Thank you for your reply, please bare with me as my experience in scripts and coding are very lacking. I realized after posting I need to save more than one certificate. I also ran the code you provided, but it deleted the certificates I set, it not did not keep them. Bad example, say I have twenty certificates in my store within Certificates - CurrentUser\Personal\Certificates. Of those twenty certificates, I want to keep five of them and delete the remaining certificates. How do I modify the code to not touch those five certificates, but delete the other 15? Those five certificates will be constant, I need to keep them at all times, the remaining certificates (which are always going to be different) so I just need the code to look for those five certificates, removing all others.

    0 comments No comments

  3. Anonymous
    2021-04-22T06:33:25.207+00:00

    Hi,

    If the certificate is to be retained or not is determined by "Issued To" you can try something like this

    $users = "user1","user2","user3","user4","user5"  
    Get-ChildItem Cert:\CurrentUser\My | ForEach-Object {  
        $ifkeep = $false  
        foreach($user in $users){  
            if($_.Subject -match $user){  
                $ifkeep = $true  
                break  
            }  
        }  
        if($ifkeep -eq $false){  
            Remove-Item $_  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.