PoshCode Logo PowerShell Code Repository

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

This script are used to set a random password for Active Directory users in a specified Organizational Unit. It stores the results in a csv-file.
The background for this script is a school domain needing to set random passwords for new users, and exporting the passwords to a csv-file to let the teachers disribute the first-time passwords.

  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 Active Directory users in a specified Organizational Unit. It stores the results in a csv-file.
  9. #          The background for this script is a school domain needing to set random passwords for new users, and exporting the passwords to a csv-#          #          file to let the teachers disribute the first-time passwords.
  10. #
  11. # You have a royalty-free right to use, modify, reproduce, and
  12. # distribute this script file in any way you find useful, provided that
  13. # you agree that the creator, owner above has no warranty, obligations,
  14. # or liability for such use.
  15. #
  16. # VERSION HISTORY:
  17. # 1.0 22.08.2009 - Initial release
  18. #
  19. ###########################################################################"
  20.  
  21. #Requires: Quest.ActiveRoles.ADManagement
  22.  
  23. #Creating system.random object used to generate random numbers
  24. $random = New-Object System.Random
  25. #Creating an array to store user information in
  26. $CSV = @()
  27. #Get users
  28. Get-QADUser -SearchRoot "domain.local/MyUserOU" -SizeLimit 0 | ForEach-Object {
  29. #Generate a random password for each user
  30. $password = "pwd"+($random.Next(1000,9999))
  31. #Set the password for each user
  32. Set-QADUser $_ -UserPassword $password
  33. #Select what user information we want to export to the csv-file and storing it in a variable
  34. $exportdata = Get-QADUser $_ | Select-Object name,samaccountname,company,department
  35. #Add the password as a member to $exportdata
  36. Add-Member -InputObject $exportdata -MemberType NoteProperty -Name Password -Value $password
  37. #Add the user information to the $CSV array
  38. $CSV += $exportdata
  39. }
  40. #Exporting all users to the csv-file
  41. $CSV | Export-Csv -Path "C:\export\passwordlist.csv" -Encoding unicode -NoTypeInformation

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