Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created July 14, 2015 13:05
Show Gist options
  • Save JohnLBevan/8094f45176d2f3b1b830 to your computer and use it in GitHub Desktop.
Save JohnLBevan/8094f45176d2f3b1b830 to your computer and use it in GitHub Desktop.
Test-PSCredentials: Check if an AD account's password is valid
#http://serverfault.com/questions/276098/check-if-user-password-input-is-valid-in-powershell-script
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
{
write-host "Authentication failed - please verify your username and password."
exit #terminate the script.
}
else
{
write-host "Successfully authenticated with domain $domain.name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment