ShiftNick

Find Stale User Accounts

Jan 22nd, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Variables
  2. $DaysInactive = 91  
  3. $time = (Get-Date).Adddays(-($DaysInactive))
  4. $SearchBase = "OU=Users,OU=Site,DC=Domain,DC=Local"
  5.  
  6. #Finds users defined in Searchbase that are enabled and the last log on was more than the $DaysInactive variable.
  7. Get-ADUser -SearchBase $SearchBase -Filter {LastLogonTimeStamp -lt $time -and Enabled -eq $true -and PasswordNeverExpires -eq $false} -Properties LastLogonTimeStamp |
  8.  
  9. #Sets the User account to Disabled
  10. Set-ADuser -Enabled $false
Add Comment
Please, Sign In to add comment