Test-ADUserCredentials.ps1
something exciting
Some information about the exciting thing
Table of contents generated with markdown-toc
Script
function Test-ADUserCredentials {
[CmdletBinding()]
param (
[string]$username,
[string]$password
)
begin {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
}
process {
$credentials = "$username" + "," + "$password"
$account = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([ DirectoryServices.AccountManagement.ContextType]::Domain, $env:userdomain),
$account.ValidateCredentials("$credentials")
}
end {
}
}
function Test-ADCredentials {
[CmdletBinding()]
param(
[pscredential]$Credential
)
try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
if (!$Credential) {
$Credential = Get-Credential -EA Stop
}
if ($Credential.username.split("\").count -ne 2) {
throw "You haven't entered credentials in DOMAIN\USERNAME format. Given value : $($Credential.Username)"
}
$DomainName = $Credential.username.Split("\")[0]
$UserName = $Credential.username.Split("\")[1]
$Password = $Credential.GetNetworkCredential().Password
$PC = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, $DomainName)
if ($PC.ValidateCredentials($UserName, $Password)) {
Write-Verbose "Credential validation successful for $($Credential.Username)"
return $True
}
else {
throw "Credential validation failed for $($Credential.Username)"
}
}
catch {
Write-Verbose "Error occurred while performing credential validation. $_"
return $False
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.