How to Force Windows to Clear Cached UNC Credentials and Reconnect with Different User Account


14 views

When working with Windows UNC paths, the credential caching mechanism can be both a convenience and a frustration. Here's what happens in the scenario:

# Initial connection (creates credential cache)
net use \\DS412\IPC$ /user:username1 *
# Disconnect attempt
net use \\DS412\IPC$ /delete

Windows maintains multiple credential stores that aren't always cleared by simple net use commands:

  • Credential Manager (Windows Vault)
  • Session-level cached credentials
  • Persistent connection history

Method 1: Command Line Nuclear Option

# List all connections (even hidden ones)
net use * /delete /y

# Alternative using PowerShell
Get-SmbConnection | Remove-SmbConnection -Force
Clear-SmbClientNetworkConnection -Force

Method 2: Credential Manager Cleanup

For Windows 7 specifically:

# Via command line (requires elevation)
cmdkey /list | findstr DS412
cmdkey /delete:DS412
cmdkey /delete:TERMSRV/DS412

Method 3: Registry Edit (When All Else Fails)

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Network\DS412]
"ConnectionType"=dword:00000001
"DeferFlags"=dword:00000004

After clearing cached credentials, establish new connection properly:

# Explicit credential specification
net use \\DS412\share /user:domain\username2 * /persistent:no

# PowerShell alternative
New-SmbMapping -RemotePath \\DS412\share -UserName "domain\username2" -Password "P@ssw0rd"

For developers needing frequent credential switches:

function Reset-NetworkCredential {
    param(
        [string]$ComputerName,
        [string]$SharePath
    )
    
    try {
        net use \\$ComputerName\IPC$ /delete /y
        cmdkey /delete:$ComputerName
        net use $SharePath /user:$env:USERDOMAIN\$env:USERNAME /persistent:no
    }
    catch {
        Write-Warning "Credential reset failed: $_"
    }
}

When troubleshooting stubborn connections:

  • Use klist purge to clear Kerberos tickets
  • Check Event Viewer for System log errors
  • Test with IP address instead of hostname

Windows aggressively caches network share credentials, which becomes problematic when you need to:

  • Switch between different user accounts on the same NAS/SMB server
  • Test permission configurations with various credentials
  • Troubleshoot authentication issues

While NET USE /DELETE works for active connections, Windows maintains credentials in these locations:

1. Credential Manager (Control Panel → User Accounts → Credential Manager)
2. Security Support Provider (SSP) cache
3. Server-side session cache

For Windows 7/10/11, use this PowerShell script to fully clear cached credentials:

# Method 1: Clear specific credential
cmdkey /delete:TERMSRV/DS412

# Method 2: Nuclear option - clear all stored credentials
cmdkey /list | ForEach-Object {
    if($_ -match "Target: (.*)"){
        cmdkey /delete:$($matches[1])
    }
}

# Flush DNS and NetBIOS cache
ipconfig /flushdns
nbtstat -R

After clearing cached credentials, use these methods to establish a fresh connection:

Command Line Approach

NET USE \\DS412\IPC$ /DELETE
NET USE \\DS412\ShareName /USER:domain\username password

Programmatic Access (C# Example)

using System;
using System.Net;

class Program {
    static void Main() {
        NetworkCredential credentials = new NetworkCredential(
            "newuser", 
            "password", 
            "domain");
            
        CredentialCache credCache = new CredentialCache();
        credCache.Add(
            new Uri(@"\\DS412\ShareName"),
            "Basic",
            credentials);
            
        // Use with System.IO or other file operations
    }
}

For one-off access without credential caching:

runas /netonly /user:domain\username "explorer.exe \\DS412\ShareName"

When troubleshooting persistent connection issues:

  • Check active sessions with: NET SESSION
  • View server-side connections: NET FILE
  • Inspect Kerberos tickets: klist purge

For automation scenarios, create this .reg file:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa]
"DisableDomainCreds"=dword:00000001
"RestrictAnonymous"=dword:00000001

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters]
"AllowInsecureGuestAuth"=dword:00000000

Remember to restart the workstation service after changes:

NET STOP WORKSTATION /Y
NET START WORKSTATION