Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Remove Expired Certs in LocalMachine\My remotely using Powershell

587 views
Skip to first unread message

Armando Torres

unread,
Oct 5, 2015, 4:28:20 PM10/5/15
to
Any help will be appreciated.
I have hundred of servers with Certs in the LocalMachine\My Store.
I can see the Cert opening a Session and Running Get-ChildItem cert:\Localmachine\my.
Is their a way that I removed only the Cert that are expired using a powershell script?
I have a list of servers in a text file.

I have tried this with no success:

$today = Get-Date
$session = New-PsSession -ComputerName TestServer
Invoke-Command -ComputerName $session { Get-ChildItem -Path Cert:\LocalMachine\My } | foreach { If ( $_.NotAfter -lt $today) { $_.Remove($Cert) }
}

Thanks!

Tom

unread,
Oct 6, 2015, 2:04:49 AM10/6/15
to
I don't know if this is still true, but I read that the cert provider in powershell is readonly.

http://www.leeholmes.com/blog/2007/08/23/removing-certificates-from-the-certificate-store/

So you have to create a store object first.

$store = new-object system.security.cryptography.x509certificates.x509Store 'My','LocalMachine'
$store.Open('ReadWrite')

Invoke-Command [...]

$store.close

Tom

unread,
Oct 6, 2015, 2:09:44 AM10/6/15
to
Since Powershell 3.0 this method should work for you

Get-ChildItem -Path cert:\LocalMachine\WebHosting -ExpiringInDays 0 | Remove-Item

https://technet.microsoft.com/en-us/library/hh847855%28v=wps.620%29.aspx

ATG

unread,
Oct 6, 2015, 9:47:27 AM10/6/15
to
I am running powershell version 4.
I tried using Get-ChildItem -Path cert:\LocalMachine\My -ExpiringInDays 0 | Remove-Item; and get the following error:
Get-ChildItem : A parameter cannot be found that matches parameter name 'ExpiringInDays'.
At line:1 char:43

Tom

unread,
Oct 7, 2015, 2:22:36 AM10/7/15
to
Powershell overrides the Get-Childitem cmdlet in the cert-store. So you have to set your location to cert: first.

Set-Location cert:
or
cd cert:
0 new messages