Skip to content

Instantly share code, notes, and snippets.

@4piu
Created May 30, 2024 20:37
Show Gist options
  • Save 4piu/26d1a965aeec0377cb25b679b93de4ae to your computer and use it in GitHub Desktop.
Save 4piu/26d1a965aeec0377cb25b679b93de4ae to your computer and use it in GitHub Desktop.
Delete Windows credential with specified regex pattern
# Prompt the user for the pattern
$pattern = Read-Host "Delete Credential with Regex pattern"
# Filter credentials that start with the specified prefix
$regex = "^ Target: (?:LegacyGeneric:target=)*" + $pattern + "$"
$creds = cmdkey /list | Select-String -Pattern $regex
# Remove each credential that starts with the specified prefix
foreach ($cred in $creds) {
$target = $cred -replace "^\s*Target: ", ""
Write-Output "Removing credential: $target"
cmdkey /delete:$target
}
Write-Output "All credentials match '$pattern' have been removed."
Write-Host -NoNewLine 'Press any key to exit...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment