Skip to content

Instantly share code, notes, and snippets.

@janikvonrotz
Created December 6, 2013 07:39
Show Gist options
  • Select an option

  • Save janikvonrotz/7819990 to your computer and use it in GitHub Desktop.

Select an option

Save janikvonrotz/7819990 to your computer and use it in GitHub Desktop.
PowerShell: Delete all cmdkey credentials #PowerShell #Windows
cmdkey /list | ForEach-Object{if($_ -like "*Ziel:*"){cmdkey /del:($_ -replace " ","" -replace "Ziel:","")}}
@JohnRanger

Copy link
Copy Markdown

And here the english Version:

cmdkey /list | ForEach-Object{if($_ -like "Target:"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}

@ramassin

ramassin commented Mar 3, 2015

Copy link
Copy Markdown
cmdkey /list | ForEach-Object{if($_ -like "*Destinazione:*"){cmdkey /del:($_ -replace " ","" -replace  Destinazione:","")}}

@CermakPOI

Copy link
Copy Markdown

In the english version the * are missing for the "-like":
cmdkey /list | ForEach-Object{if($_ -like "Target:"){cmdkey /delete:($_ -replace " ","" -replace "Target:","")}}

@HerrHeuschrecke

HerrHeuschrecke commented Sep 10, 2016

Copy link
Copy Markdown

Danke/grazie/thanks/děkuji! This line I needed this late night.

And yes, Cermak, those "*" were important. :)
So, correct English line:
cmdkey /list | ForEach-Object{if($_ -like "*Target:*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}

@jcmmarq

jcmmarq commented May 1, 2017

Copy link
Copy Markdown

works great. However what if you want to delete just specific Items that starts with a specific name for example I wasn't ot delete All credentials that the Target Name starts with Microsoft_OC1. I have tried using the above command with the following but does not work
cmdkey /list | ForEach-Object{if($_ -like "Target:Microsoft_OC1"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}
the object here is to only delete specific credentials not all of them.

Here is a credential that is currently stored.
Users\marquesj.AUTOCLUB> cmdkey /list

Currently stored credentials:

Target: LegacyGeneric:target=Microsoft_OC1:uri=marquesj@aaa-calif.com:specific:

LOC:1
Type: Generic
User: marquesj

@jcmmarq

jcmmarq commented May 1, 2017

Copy link
Copy Markdown

appreciate any help you can provide. Thanks!!!

@mrajusjp

mrajusjp commented Sep 18, 2017

Copy link
Copy Markdown

Try this.
cmdkey /list | ForEach-Object{if($_ -like "*Microsoft_OC1*"){cmdkey /del:($_ -replace " ","" -replace "Target:","")}}

@cpratlong

cpratlong commented Oct 15, 2018

Copy link
Copy Markdown

Here is a "filtering" allowing to erase all, whatever windows language it is

$Credentials = (cmdkey /list | Where-Object {$_ -like "*Target=*"})
Foreach ($Target in $Credentials) {
    $Target = ($Target -split (":", 2) | Select-Object -Skip 1).substring(1)
    $Argument = "/delete:" + $Target
    Start-Process Cmdkey -ArgumentList $Argument -NoNewWindow -RedirectStandardOutput $False
    }

@BigKatGalarraga

Copy link
Copy Markdown

Try this. Just tried it and it work! Used regex to remove everything the beginning of Target:....to after target=. Then pass that on to /del:$_

cmdkey /list | ForEach-Object {if ($_ -like "Target:") {cmdkey /del:($_ -replace '^.+=', '')}}

@BigKatGalarraga

Copy link
Copy Markdown

looks like the * asterisks were removed when posting. You can add them before/after the quotes to -like "Target:" or use -match "Target: "

@vinayiitkgp

Copy link
Copy Markdown

I need to delete cached credentials of a user. The tool I am using to connect to the remote computer runs under sys. So cmdkey /list is returning only the targets that it has access to. When the cmdkey /list is run on the target computer directly on the powershell console, it returns all the target that the logged user has access to. How do I retrieve these targets from a remote session running user sys?

@kobyon

kobyon commented Jan 12, 2022

Copy link
Copy Markdown

@leblancmeneses

Copy link
Copy Markdown

Target: LegacyGeneric:target=Adobe App Prefetched Info (cc)(Part2)

cmdkey /list | ForEach-Object {
  if ($_ -like "*Adobe*") {
    cmdkey /del:($_ -replace '^[^=]+',"" -replace "=","")
  }
}

@undone37

undone37 commented Feb 9, 2023

Copy link
Copy Markdown

German:
cmdkey /list | ForEach-Object{if($_ -like "*Ziel:*"){cmdkey /del:($_ -replace "    ","" -replace "Ziel: ","")}}
English:
cmdkey /list | ForEach-Object{if($_ -like "*Target:*"){cmdkey /del:($_ -replace "    ","" -replace "Target: ","")}}

This will even delete entries with spaces (based on the original version).

@Joadison

Copy link
Copy Markdown

O comando é cmdkey /list | ForEach-Object {if($_ -like "Target=") {echo($_ -replace " ","" -replace "^.*target=","")}}

@Joadison

Copy link
Copy Markdown

O comondo correto é

cmdkey /list | ForEach-Object {if($_ -like "Target=") {echo($_ -replace " ","" -replace "^.*target=","")}}

@Joadison

Copy link
Copy Markdown

cmdkey /list | ForEach-Object {if($_ -like "*Target=*") {cmdkey /delete:($_ -replace " ","" -replace "^.*target=","")}}

@lucapiccio

lucapiccio commented Mar 18, 2024

Copy link
Copy Markdown

For me all your listing depends from windows language version.

If you want to delete all key ignoring the lang of windows you can do:

cmdkey /list | ForEach-Object{
    if($_ -like "*=*"){
        $c = ($_ -replace (' ')).split(":", 2)[1]
        cmdkey.exe /delete $c | Out-Null
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment