private775 icon

AD: Check if AD user account is locked

private775 | PRO | 11/21/16 07:15:07 AM UTC | 0 ⭐ | 566 👁️ | Never ⏰ | []
PowerShell |

870 B

|

None

|

0 👍

/

0 👎

# taken from http://www.alexwinner.com/articles/powershell/111-lockoutaccountsad.html
 
function checkIfLocked($sAMAccountName){
    # sAMAccountName of the user
    # $sAMAccountName = "User-0"
     
    $ADS_UF_LOCKOUT = 16
     
    $Attribute = "msds-user-account-control-computed"
     
    $ADSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $ADSearcher.PageSize = 1000
    $ADSearcher.Filter = "samaccountname=$sAMAccountName"
    $User = $ADSearcher.FindOne()
     
    # Use DirectoryEntry and RefreshCache method
    $MyUser = $User.GetDirectoryEntry()
    $MyUser.RefreshCache($Attribute)
     
    # Return the value of msds-user-account-control-computed
    $UserAccountFlag = $MyUser.Properties[$Attribute].Value
     
    if ( $UserAccountFlag -band $ADS_UF_LOCKOUT ) {
        Write-host "Account $sAMAccountName is locked"
    } else {
        Write-host "Account $sAMAccountName is NOT locked"
    }
}

Comments