private775

AD: Check if AD user account is locked

Nov 21st, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # taken from http://www.alexwinner.com/articles/powershell/111-lockoutaccountsad.html
  2.  
  3. function checkIfLocked($sAMAccountName){
  4.     # sAMAccountName of the user
  5.     # $sAMAccountName = "User-0"
  6.      
  7.     $ADS_UF_LOCKOUT = 16
  8.      
  9.     $Attribute = "msds-user-account-control-computed"
  10.      
  11.     $ADSearcher = New-Object System.DirectoryServices.DirectorySearcher
  12.     $ADSearcher.PageSize = 1000
  13.     $ADSearcher.Filter = "samaccountname=$sAMAccountName"
  14.     $User = $ADSearcher.FindOne()
  15.      
  16.     # Use DirectoryEntry and RefreshCache method
  17.     $MyUser = $User.GetDirectoryEntry()
  18.     $MyUser.RefreshCache($Attribute)
  19.      
  20.     # Return the value of msds-user-account-control-computed
  21.     $UserAccountFlag = $MyUser.Properties[$Attribute].Value
  22.      
  23.     if ( $UserAccountFlag -band $ADS_UF_LOCKOUT ) {
  24.         Write-host "Account $sAMAccountName is locked"
  25.     } else {
  26.         Write-host "Account $sAMAccountName is NOT locked"
  27.     }
  28. }
Add Comment
Please, Sign In to add comment