Validating Domain Credentials

by Apr 21, 2015

To check credentials (username plus password) against your current domain, you can use this approach:

#requires -Version 1

$username = 'test\user'
$password = 'topSecret'

$root = "LDAP://" + ([ADSI]"").distinguishedName
$Domain = New-Object System.DirectoryServices.DirectoryEntry($root, $username, $password)

if ($Domain.Name -eq $null)
  Write-Warning 'Credentials incorrect, or computer is not a domain member.'
  Write-Host 'Credentials accepted.'

In a nutshell, the script determines the distinguished name of your current domain, then tries to retrieve the root element using the supplied credentials.

If this succeeds, the credentials are valid. If it does not succeed, the credentials are either invalid, or your computer is not domain joined in the first place.

Twitter This Tip! ReTweet this Tip!