Change TERMSERV logins in Windows Credential Manager

This script will delete and then re-add windows credential manager entries via the cmdkey command line utility.  I use this when I have to change windows account login to update all the termserv entries so I don’t have to save them all again.  Use at your own risk. 

cls
$objects = @();
$select = @('Target:', 'Type:', 'User:')
[int]$counter = 0;

$obj = New-Object -TypeName PSObject -Property @{
    Target = ''
    Type = ''
    User = ''
}

$creds = cmdkey /list | Select-String $select
$creds | %{
    $counter++;

    [string]$line = $_.ToString()

    switch -Wildcard ($_){
        "*Target:*"{
            $obj.Target = $line.Substring($line.IndexOf(':')+2, $line.Length - $line.IndexOf(':')-2)
            break;
        }
        "*Type:*"{
            $obj.Type = $line.Substring($line.IndexOf(':')+2, $line.Length - $line.IndexOf(':')-2)
            break;
        }
        "*User:*"{
            $obj.User = $line.Substring($line.IndexOf(':')+2, $line.Length - $line.IndexOf(':')-2)
            break;
        }
        
    }

    if($counter % 3 -eq 0){
        $objects += $obj;
        $obj = New-Object -TypeName PSObject -Property @{
            Target = ''
            Type = ''
            User = ''
        }
    }

}

$userName = 'domain\login'
$password = 'password'

$objects | where{$_.Target -like "*TERMSRV/*"} | %{
    $string = $_.Target
    $string = $string.Substring($string.LastIndexOf("=")+1)
    $cmd = "cmdkey /delete:$string"
    Invoke-Expression -Command $cmd
    $cmd = "cmdkey /add:$string /user:$userName /pass:$password"
    Invoke-Expression -Command $cmd
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.