How to Clear Cached Domain Credentials in Windows for Testing Wireless Domain Authentication


2 views

When a Windows machine joins a domain, it caches domain credentials locally to allow users to log in even when the domain controller is unavailable. This feature, while convenient for most scenarios, can become problematic when testing wireless domain authentication setups where you need fresh authentication attempts.

The simplest way to clear cached credentials is through the Control Panel:

  1. Open Control Panel > User Accounts > Credential Manager
  2. Select "Windows Credentials"
  3. Remove any domain credentials listed under "Generic Credentials"

For automation or scripting purposes, you can use the following command:

cmdkey /delete:Domain:target=domain.com

To list all cached credentials first:

cmdkey /list

For a more thorough cleanup, you can modify the registry to clear all cached credentials:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SECURITY\Policy\Secrets]
"NL$KM"=hex:

Warning: Editing the registry incorrectly can cause serious problems. Always back up the registry before making changes.

Here's a more sophisticated PowerShell script that handles the process safely:

# Clear cached domain credentials
function Clear-CachedCredentials {
    try {
        $key = "HKLM:\SECURITY\Policy\Secrets\NL$KM"
        if (Test-Path $key) {
            Remove-ItemProperty -Path $key -Name "(default)" -ErrorAction Stop
            Write-Host "Cached credentials cleared successfully."
        } else {
            Write-Warning "Cached credentials key not found."
        }
    }
    catch {
        Write-Error "Failed to clear cached credentials: $_"
    }
}

Clear-CachedCredentials

After clearing the credentials, you can properly test wireless domain authentication by:

  1. Disconnecting from the wired network
  2. Connecting to the wireless network
  3. Attempting to log in with domain credentials
  4. Verifying authentication logs on the domain controller

Remember that clearing cached credentials means:

  • The machine won't be able to log in without contacting a domain controller
  • This should only be done on test machines, not production systems
  • Consider creating a dedicated test account rather than using production accounts

Windows caches domain credentials in the local Security Account Manager (SAM) database to allow offline logins. This behavior is controlled by the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon registry key, specifically the CachedLogonsCount value.

For one-off clearing:

# PowerShell command to list cached credentials
klist tickets

# To purge all cached credentials
klist purge

# Alternative cmd command:
rundll32.exe keymgr.dll,KRShowKeyMgr

For repeated testing scenarios, create a PowerShell script:

# ClearCredCache.ps1
function Clear-CachedCredentials {
    try {
        $service = Get-Service -Name "SamSs" -ErrorAction Stop
        $service.Stop()
        Start-Sleep -Seconds 3
        Remove-Item "HKLM:\SECURITY\Cache" -Recurse -Force
        $service.Start()
        Write-Output "Cached credentials successfully cleared"
    }
    catch {
        Write-Error "Failed to clear cache: $_"
    }
}

Clear-CachedCredentials

To temporarily disable caching during testing:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v CachedLogonsCount /t REG_SZ /d 0 /f

Remember to set it back to default (10) when done.

After clearing cache, verify with this network test script:

# Test-WirelessAuth.ps1
$cred = Get-Credential
Test-ComputerSecureChannel -Credential $cred -Server "yourDC.domain.com"

When scripting credential operations:

  • Always run with elevated privileges
  • Log operations for audit trails
  • Consider implementing a rollback mechanism

For system administrators managing multiple machines:

$computers = "PC1","PC2","PC3"
Invoke-Command -ComputerName $computers -ScriptBlock {
    & klist purge | Out-Null
}