I want to create a powershell script that will remove expired certificates but I keep receiving an error.
I also changed the notafter property to display as expiration date.
$today = Get-Date
dir Cert:\LocalMachine\My\|
select thumbprint, subject, @{Name="ExpirationDate";Expression=
{$_.NotAfter}}|
Where-Object ExpirationDate -lt $today|
Remove-Item
Remove-Item : Cannot find drive. A drive with the name '@{Thumbprint=XXXX;
Subject=CN=xyz.org, OU=X, O=X, L=X, S=X,
C=US; NotAfter=X' does not exist.
At C:\Users\Documents\Delete Expired Certs Script.ps1:10 char:2
+ Remove-Item
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (@{Thumbprint=70...r=:String) [Remove-Item], DriveNotFoun
dException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
select
part. The pipeline should be as follows:dir Cert:\LocalMachine\My\| Where-Object NotAfter -lt $today | Remove-Item
– JosefZ Jan 30 '19 at 19:02Remove-Item
against apscustomobject
they crafted withSelect-Object
. Their syntax withWhere-Object
suggests they're on PSv3+. Voting to close the question as off-topic: syntax error. – Maximilian Burszley Jan 30 '19 at 19:21Remove-Item
and that provider that exists in 2.0. However... I didn't notice that the object was changed in the pipeline either so likely that is the first culprit. – Matt Jan 30 '19 at 19:34