Skip to content

Instantly share code, notes, and snippets.

@dpo007
Last active August 18, 2020 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpo007/374850746dfa8d5d158e5841bf56519d to your computer and use it in GitHub Desktop.
Save dpo007/374850746dfa8d5d158e5841bf56519d to your computer and use it in GitHub Desktop.
PowerShell :: Function to check password against Windows Domain rules.
function Test-PasswordForDomain {
param (
[Parameter(Mandatory=$true)]
[string]$Password,
[Parameter(Mandatory=$false)]
[string]$AccountSamAccountName = "",
[Parameter(Mandatory=$false)]
[string]$AccountDisplayName,
[Microsoft.ActiveDirectory.Management.ADEntity]$PasswordPolicy = (Get-ADDefaultDomainPasswordPolicy -ErrorAction SilentlyContinue)
)
[string]$script:FailReason = $null
If ($Password.Length -lt $PasswordPolicy.MinPasswordLength) {
$script:FailReason = 'Too Short.'
return $false
}
if (($AccountSamAccountName) -and ($Password -match "$AccountSamAccountName")) {
$script:FailReason = 'Contains SAM Account Name.'
return $false
}
if ($AccountDisplayName) {
$tokens = $AccountDisplayName.Split(",.-,_ #`t")
foreach ($token in $tokens) {
if (($token) -and ($Password -match "$token")) {
$script:FailReason = 'Contains part of Display Name.'
return $false
}
}
}
if ($PasswordPolicy.ComplexityEnabled -eq $true) {
If (($Password -cmatch "[A-Z\p{Lu}\s]") `
-and ($Password -cmatch "[a-z\p{Ll}\s]") `
-and ($Password -match "[\d]") `
-and ($Password -match "[^\w]")
) {
return $true
} else {
$script:FailReason = 'Does not meet basic Windows complexity requirements.'
return $false
}
}
return $true
}
# Example
If (!(Test-PasswordForDomain "Apass!") {
Write-Host 'Failed : ' + $FailReason
} else {
Write-Host 'Passed'
}
@dpo007
Copy link
Author

dpo007 commented Aug 18, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment