PoshCode Logo PowerShell Code Repository

Set-ADUserRandomPassword by Jan Egil Ring 7 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1351"></script>download | new post

  1. ###########################################################################"
  2. #
  3. # NAME: Set-ADUserRandomPassword.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # EMAIL: jan.egil.ring@powershell.no
  7. #
  8. # COMMENT: This script are used to set a random password for the Active Directory user with the username provided by the user who runs the script.
  9. #          The password are set to a random password, and "User must change password at next logon" are enabled.
  10. #          At last the displayname,username,company-name,department-name and the new password are displayed.
  11. #          Script logic to check if the provided username exist are added.
  12. #          
  13. #
  14. # You have a royalty-free right to use, modify, reproduce, and
  15. # distribute this script file in any way you find useful, provided that
  16. # you agree that the creator, owner above has no warranty, obligations,
  17. # or liability for such use.
  18. #
  19. # VERSION HISTORY:
  20. # 1.0 29.09.2009 - Initial release
  21. #
  22. ###########################################################################"
  23.  
  24. #requires -pssnapin Quest.ActiveRoles.ADManagement
  25.  
  26. #Creating system.random object used to generate random numbers
  27. $random = New-Object System.Random
  28.  
  29. #Set searchroot
  30. $searchroot = "domain.local/Example-OU"
  31.  
  32. #Get username for the user to reset password for
  33. $username = Read-Host "Enter username for the user you want to change password for:"
  34. $userobject = Get-QADUser $username -SearchRoot $searchroot
  35.  
  36.  
  37. if ($userobject -ne $null) {
  38. #User exist, continue
  39. }
  40. else
  41. {
  42. #User does not exist, ask user to enter username again
  43. do { $username = Read-Host "There are no users with the username $username. Re-enter username.";$userobject = Get-QADUser $username -SearchRoot $searchroot } until ($userobject -ne $null)
  44. $userobject = Get-QADUser $username -SearchRoot $searchroot
  45. }
  46.  
  47. #Generate a random password for each user
  48. $password = "Pwd"+($random.Next(1000,9999))
  49.  
  50. #Set the password for each user
  51. Set-QADUser $userobject -UserPassword $password -UserMustChangePassword $true | Out-Null
  52.  
  53. #Select what user information we want to export to the csv-file and storing it in a variable
  54. $userdata = Get-QADUser $userobject | Select-Object @{Name="Name"; Expression = {$_.displayname}},@{Name="Username"; Expression = {$_.samaccountname}},@{Name="Company-name"; Expression = {$_.company}},@{Name="Department-name"; Expression = {$_.department}}
  55.  
  56. #Add the password as a member to $userdata
  57. Add-Member -InputObject $userdata -MemberType NoteProperty -Name "New password" -Value $password -Force
  58.  
  59. #Feedback
  60. Write-Host "The password-change was successfully for the following user:"
  61. $userdata
  62. Write-Host "Note: The user must change the password on the next logon."

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me