gangstanthony

inactive-aduser-query

Jun 1st, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # http://stackoverflow.com/questions/37577369/inactive-aduser-query-and-adgroup-manipulation-encountering-issues
  2.  
  3. # Import Modules Needed
  4. Import-Module ActiveDirectory
  5.  
  6. # Output results to CSV file
  7. $LogFile = "C:\ZombieAcct_90dayRpt_n_Move.csv"
  8.  
  9. # Today's Date
  10. $today = get-date -uformat "%Y/%m/%d"
  11.  
  12. # Date to search by
  13. $xDays = (get-date).AddDays(-90)
  14.  
  15. # Expiration date
  16. $expire = (get-date).AddDays(-1)
  17.  
  18. # Date disabled description variable
  19. $userDesc = "Disabled Inactive" + " - " + $today
  20.  
  21. # Sets the OU to do the base search for all user accounts, change as required
  22. $SearchBase = "OU=DEFINE,OU=DEFINE,OU=DEFINE,DC=DEFINE,DC=com"
  23.  
  24. # Sets the OU where accounts will be MOVED TO, change as required
  25. $ParkingOU = "OU=30Days, OU=Disabled Accounts, OU=Domain Services, DC=DEFINE, DC=com"
  26.  
  27. # Pull all inactive users older than 90-days from a specified OU
  28. $Users = Get-ADUser -SearchBase $SearchBase -Properties memberof, LastLogonDate, PasswordLastSet, PasswordNeverExpires, WhenCreated, DisplayName -Filter {
  29.     (LastLogonDate -notlike '*' -OR LastLogonDate -le $xDays)
  30.     -AND (PasswordLastSet -le $xDays)
  31.     -AND (Enabled -eq $True)
  32.     -AND (PasswordNeverExpires -eq $false)
  33.     -AND (WhenCreated -le $xDays)
  34. } | ForEach-Object {
  35.     Set-ADUser $_ -AccountExpirationDate $expire -Description $userdesc -WhatIf
  36.     Move-ADObject $_ -TargetPath $ParkingOU -WhatIf
  37.     $_ | select DisplayName, Name, SAMaccountName, PasswordExpired, PasswordNeverExpires, WhenCreated, PasswordLastSet, LastLogonDate, @{n='Groups';e={(($_.memberof | Get-ADGroup).Name) -join '; '}}
  38. }
  39.  
  40. $Users | Export-Csv $LogFile -NoTypeInformation
  41.  
  42. start $LogFile
Advertisement
Add Comment
Please, Sign In to add comment