Here’s one way to wire everything together in PowerShell:
1) Watch for the “smart-card driver unloaded” event (Kernel-PnP EventID 1010, scfilter)
2) On trigger, enumerate—and delete—the certs in Cert:\CurrentUser\My whose Issuer matches your criteria
1) Create a Scheduled Task (simplest, “no always-running console” approach)
- Open Task Scheduler → Create Task…
- General
- Name: “Purge-Smartcard-Certs”
- Run only when user is logged on (so you can access Cert:\CurrentUser)
- Triggers → New…
- Begin the task: On an event
- Log: Microsoft-Windows-Kernel-PnP/Configuration
- Source: Kernel-PnP
- Event ID: 1010
- Actions → New…
- Conditions/Settings → disable “Stop if the task runs longer than …” so your cleanup script can finish.
2) The cleanup script
Save this as C:\Scripts\PurgeSmartcardCerts.ps1:
#— Customize this to match the Issuer or thumbprint you want to purge
$targetIssuer = 'CN=MySmartcardCA, O=MyOrg, C=US'
# Optional: change to your log file
$logFile = "C:\Scripts\PurgedCerts.log"
function Log {
Param($msg)
$time = (Get-Date).ToString('u')
"$time`t$msg" | Out-File -FilePath $logFile -Append -Encoding utf8
}
try {
Log "=== Smartcard removal detected; purging certs where Issuer = '$targetIssuer'"
# Enumerate & remove
$removed = Get-ChildItem Cert:\CurrentUser\My `
| Where-Object { $_.Issuer -eq $targetIssuer } `
| ForEach-Object {
try {
Remove-Item -Path $_.PSPath -Force -ErrorAction Stop
Log "REMOVED: $($_.Thumbprint) – $($_.Subject)"
}
catch {
Log "ERROR removing $($_.Thumbprint): $($_.Exception.Message)"
}
}
if (-not $removed) {
Log "No matching certs found."
}
}
catch {
Log "Fatal error: $($_.Exception.Message)"
}
What it does:
- Opens Cert:\CurrentUser\My
- Filters on
Issuer -eq $targetIssuer
Remove-Item -Force each match
- Logs successes/failures to a
.log
3) (Alternative) “Always-on” PowerShell event subscription
If you’d rather have a single PS console or background job instead of a Scheduled Task:
# Define your cleanup action
$action = {
$targetIssuer = 'CN=MySmartcardCA, O=MyOrg, C=US'
Get-ChildItem Cert:\CurrentUser\My |
Where-Object Issuer -eq $targetIssuer |
Remove-Item -Force -ErrorAction SilentlyContinue
}
# WMI query for any PnP-entity deletion whose name contains "scfilter"
$query = @"
SELECT * FROM __InstanceDeletionEvent
WITHIN 2
WHERE TargetInstance ISA 'Win32_PnPEntity'
AND TargetInstance.Name LIKE '%SCFilter%'
"@
# Register the event
Register-WmiEvent -Query $query `
-SourceIdentifier 'SmartcardRemoved' `
-Action $action
Write-Host "Listening for smartcard removal… (Press Ctrl+C to quit)"
while ($true) { Start-Sleep -Seconds 5 }
Notes:
WITHIN 2 polls every 2 seconds.
- You’ll need to run this in a console that stays open (or wrap in a background job).
Testing
- Insert your smartcard → watch no log entries.
- Remove the card → you should see a new entry in your
.log (or in console if you’re using the WMI subscription).
- Verify that
Get-ChildItem Cert:\CurrentUser\My no longer shows the purged cert(s).
—
Feel free to tweak the issuer filter (you can use -like '*MyCA*' or check subject, thumbprint, EKU, etc.) and the logging path. This pattern can be generalized to run any post-removal cleanup. 👍