Test Active Directory User Accounts for a Default Password with PowerShell

How do you control password resets in your environment? I’ve worked for numerous companies where their forgotten password reset process was all over the board. Hopefully you have a process in place that allows you to sleep at night. Even with the best policies and procedures in place, what happens when someone on your help desk staff resets a users password to some default password and forgets to set the account so the password has to be changed at next logon? Is the user still using that default password weeks later?

I decided to write a PowerShell script to test user accounts for just that exact scenario.

#Requires -Version 3.0 -Modules ActiveDirectory
function Test-MrADUserPassword {

<#
.SYNOPSIS
    Test-MrADUserPassword is a function for testing an Active Directory user account for a specific password.

.DESCRIPTION
    Test-MrADUserPassword is an advanced function for testing one or more Active Directory user accounts for a
    specific password.

.PARAMETER UserName
    The username for the Active Directory user account.

.PARAMETER Password
    The password to test for.

.PARAMETER ComputerName
    A server or computer name that has PowerShell remoting enabled.

.PARAMETER InputObject
    Accepts the output of Get-ADUser.

.EXAMPLE
     Test-MrADUserPassword -UserName alan0 -Password Password1 -ComputerName Server01

.EXAMPLE
     'alan0'. 'andrew1', 'frank2' | Test-MrADUserPassword -Password Password1 -ComputerName Server01

.EXAMPLE
     Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' |
     Test-MrPassword -Password Password1 -ComputerName Server01

.INPUTS
    String, Microsoft.ActiveDirectory.Management.ADUser

.OUTPUTS
    PSCustomObject

.NOTES
    Author:  Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
#>

    [CmdletBinding(DefaultParameterSetName='Parameter Set UserName')]
    param (
        [Parameter(Mandatory,
                   ValueFromPipeline,
                   ValueFromPipelineByPropertyName,
                   ParameterSetName='Parameter Set UserName')]
        [Alias('SamAccountName')]
        [string[]]$UserName,

        [Parameter(Mandatory)]
        [string]$Password,

        [Parameter(Mandatory)]
        [string]$ComputerName,

        [Parameter(ValueFromPipeline,
                   ParameterSetName='Parameter Set InputObject')]
        [Microsoft.ActiveDirectory.Management.ADUser]$InputObject

    )

    BEGIN {
        $Pass = ConvertTo-SecureString $Password -AsPlainText -Force

        $Params = @{
            ComputerName = $ComputerName
            ScriptBlock = {Get-Random | Out-Null}
            ErrorAction = 'SilentlyContinue'
            ErrorVariable  = 'Results'
        }
    }

    PROCESS {
        if ($PSBoundParameters.UserName) {
            Write-Verbose -Message 'Input received via the "UserName" parameter set.'
            $Users = $UserName
        }
        elseif ($PSBoundParameters.InputObject) {
            Write-Verbose -Message 'Input received via the "InputObject" parameter set.'
            $Users = $InputObject
        }

        foreach ($User in $Users) {

            if (-not($Users.SamAccountName)) {
                Write-Verbose -Message "Querying Active Directory for UserName $($User)"
                $User = Get-ADUser -Identity $User
            }

            $Params.Credential = (New-Object System.Management.Automation.PSCredential ($($User.UserPrincipalName), $Pass))

            Invoke-Command @Params

            [pscustomobject]@{
                UserName = $User.SamAccountName
                PasswordCorrect =
                    switch ($Results.FullyQualifiedErrorId -replace ',.*$') {
                        LogonFailure {$false; break}
                        AccessDenied {$true; break}
                        default {$true}
                    }
            }

        }

    }

}

Test one or more Active Directory user accounts for a password of “Password1”:

Test-MrADUserPassword -UserName alan0, david1 -Password Password1 -ComputerName web01

test-password1a.png

Same test except using pipeline input of strings for the usernames:

'alan0', 'david1' | Test-MrADUserPassword -Password Password1 -ComputerName web01

test-password2a.png

Test all of the user accounts in a specific OU (Organizational Unit) in Active Directory:

Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' |
Test-MrPassword -Password Password1 -ComputerName web01 |
Sort-Object -Property PasswordCorrect

test-password3a.png

Same as the previous example except only return the accounts where the password matched.

Get-ADUser -Filter * -SearchBase 'OU=AdventureWorks Users,OU=Users,OU=Test,DC=mikefrobbins,DC=com' |
Test-MrPassword -Password Password1 -ComputerName web01 |
Where-Object PasswordCorrect -eq $true

test-password4a.png

Be sure to test this and to get permission from someone in your chain of command before running it in a production environment. Be careful when using this function because it does count as a failed login for the user account if the password doesn’t match. It will show up on your audit login failures report if you’re performing any type of auditing for login failures. You could also end up locking out the user account if you run this enough times to meet the account lockout threshold set in your domain or in the fine grained password policies if they’re enabled in your environment.

The function shown in this blog article can be downloaded from my ActiveDirectory repository on GitHub.


Update:

While at the PowerShell + DevOps Global Summit this week, I was discussing this function with a group of attendees and I discovered that there’s a better way to accomplish this task. I’ll post a follow-up blog article next week.

ยต