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 {

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

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

    The username for the Active Directory user account.

    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.

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

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

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

    String, Microsoft.ActiveDirectory.Management.ADUser


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

    [CmdletBinding(DefaultParameterSetName='Parameter Set UserName')]
    param (
                   ParameterSetName='Parameter Set UserName')]



                   ParameterSetName='Parameter Set InputObject')]


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

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

        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

                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


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

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


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


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


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.


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.