profile for Hannel on Stack Exchange, a network of free, community-driven Q&A sites
Project Work Hours
Sat & Sun 6am - 9am
Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


I was looking for this information a week ago and it seems to be missing on the web so now that i have figured it out, it time to share.



This script is designed to check the computer’s credential manager for certain credentials you specify in the filter and delete them, as simple as that.

This is useful for mass deployment where script can be pushed via Group Policy Objects (GPO) or any other deployment tools, not really needed for small environment. Due to numerous different deployment tools i have created the script in two format Batch (CMD) and Powershell (PS1).

Before running or apply the script to a group of computers please make sure they meet the requirement to have the CMDKey command.

If you can’t copy and paste script here


Powershell (PS1)

#Clearing Credential Manager
#Hannel Hazeley
#Version 2.0

#Set filters to query cerdential manager for
$filters = "TestScript*"

#Extract information from Credential Manager and filter only Target.
Foreach ($filter in $filters)
$keys = cmdkey /list:($filter) | Findstr "Target"
$keys = ($keys -replace " ","" -replace "Target:","")

#Delete each target
Foreach ($key in $keys)
Write-Host "Removing credentials for target $key"
Write-host cmdkey /del:($key) #for script to actually work remove 'Write-host' 

Batch (CMD)

@echo off
set Filter=TestScript
for /f "tokens=1*" %%a in ('cmdkey.exe /list ^| find /i "%Filter%"') do (
echo Removing credentials for target '%%~b'
:: Remove 'echo' from line below for script to work

echo cmdkey.exe /delete:"%%~b"