& Lt; Power Shell & gt; 10 Delete expired certificates in the personal storage

Source: Internet
Author: User

HI, good morning. cantgis has met you again.
Today, let's talk about the certificate. We use a lot of website verification certificates, including banks and entertainment websites. They will expire once a year.
In the certificate list in Internet Explorer, the set of expired certificates exists irregularly. How can we query and obtain and process them? The following cantgis provides you with this solution.

To obtain and find these certificates, we need to use the x.509 certificate providerMicrosoft. PowerShell. Security \ Certificate)
In powershell2.0, We need to manually clear these, X509store class objects, and use get-item:

$myCerts = Get-Item Cert:\CurrentUser\My


Next, to delete the certificate, you must open the x509 Certificate storage object through enumeration: openflags. This open () method: you can create a new storage or set access to the specified storage, of course, based on openflags)
By default, this store is read-only and cannot be deleted.


Member name Description

IncludeArchived Open the X.509 certificate store and add an archived certificate.

MaxAllowed Open the X.509 certificate store in the form that allows the highest level of access.

OpenExistingOnly Only open an existing bucket. If no storage area exists, the Open method does not create a new storage area.

ReadOnly Open the X.509 certificate store in read-only mode.

ReadWrite Open the X.509 Certificate storage area in read/write mode.

OK. We use

$myCerts.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)


Use the following cmdlet to filter out the certificates that expired yesterday. $ mycerts has already pointed to the path. We can use it for reference, instead of making repeated encoding useless strings.

$today = Get-Date$ExpiredList = Get-ChildItem $myCerts.PSPath | Where-Object { $_.NotAfter -lt $today }


We don't want to call the pipe: where-object cmdlet command every time, because we don't need this
Delete the x.509 Certificate storage area of the certificate, and then execute the query.

ForEach ($Cert in $ExpiredList) {   $myCerts.Remove($Cert)}                                                                         $myCerts.Close() # We opened it, so we need to close it.



In powershell 3.0, we can integrate and execute

$ Today = Get-DateGet-ChildItem Cert: \ CurrentUser \ My | Where-Object NotAfter-lt $ today | Remove-Item # or Get-ChildItem Cert: \ CurrentUser \ My | ForEach-Object-begin {$ now = get-date}-process {if ($ PSItem. notAfter-lt $ now) Remove-Item


This article is from the "Cantgis" blog, please be sure to keep this source http://cantgis.blog.51cto.com/5788192/1228707

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.