Last active
August 18, 2020 16:16
-
-
Save dpo007/374850746dfa8d5d158e5841bf56519d to your computer and use it in GitHub Desktop.
PowerShell :: Function to check password against Windows Domain rules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Credit/more info: https://www.checkyourlogs.net/active-directory-password-complexity-check-powershell-mvphour/