Created
May 30, 2024 20:37
-
-
Save 4piu/26d1a965aeec0377cb25b679b93de4ae to your computer and use it in GitHub Desktop.
Delete Windows credential with specified regex pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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