Advertisement
TimSutton

RetiredUsers v2

Jan 8th, 2014
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # RetiredUser.ps1
  2. # Description - script to automate basic retiring of user account.
  3. #
  4. # Created: 06/01/04
  5. # Creator: Tim Sutton
  6. #
  7. # v2 - changed group removal method to prevent previous errors.
  8. #    - changed how description is written to include running username.
  9. #    - changed password reset to use random password.
  10. #    - added outputs for clarity.
  11. #    - added password output.
  12.  
  13.  
  14. # This is where we define the parameters.
  15. #get UserName
  16. $termuser = read-host "Enter user name to retire"
  17.  
  18.  
  19. # Clear account details
  20. Get-ADUser $termuser | Set-ADUser -Company $null -Department $null -Description $null -Fax $null -HomePhone $null -MobilePhone $null -Office $null -PostalCode $null -State $null -StreetAddress $null -City $null -OfficePhone $null -Title $null -HomePage $null
  21. write-host "*  Cleared all user account fields for" $termuser
  22.  
  23. # Remove all group memberships bar Domain Users
  24. $userGroups = (Get-ADUser $termuser -properties memberof).memberof
  25. $userGroups | Remove-ADGroupmember -Members $termuser -Confirm:$false
  26. write-host "*  Removed from all distribution and security groups from" $termuser
  27.  
  28. # Set description
  29. $termDate = get-date -uformat "%Y-%m-%d"
  30. $terminatedby = $env:username
  31. $termUserDesc = $termDate + " Moved to Retired Accounts OU - " + $terminatedby
  32. set-ADUser $termuser -Description $termUserDesc
  33. write-host "*  Description set to: " $termUserDesc
  34.  
  35.  
  36. # Set Password
  37. $length = 15
  38. $punc = 46..46
  39. $digits = 48..57
  40. $letters = 65..90 + 97..122
  41. $password = get-random -count $length -input ($punc + $digits + $letters) | % -begin { $aa = $null } -process {$aa += [char]$_} -end {$aa}
  42.  
  43. Get-ADUser $termuser |Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $password -Force)
  44. write-host "*  Reset password to: " $password
  45.  
  46.  
  47. # Move to retired ou
  48. Get-ADUser $termuser | Move-ADObject -TargetPath 'ou=retired accounts,ou=uk,dc=domain,dc=local'
  49. write-host "* " $termuser "moved to Retired Users OU"
  50.  
  51.  
  52. Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement