Maleboligia

Export OU Users to CSV with random passwords

Apr 4th, 2016
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module activedirectory
  2. # Create CSV file of users and passwords reset
  3. echo "User,Password" | Out-File -FilePath  "C:\students.csv" -Append
  4. # Get list of users from an OU
  5. $Users = Get-ADUser -Filter * -Properties * -SearchBase "ou=students,ou=Schools,dc=contoso,dc=local"
  6. foreach ($user in $Users) {
  7.     $username = $user.SAMACCOUNTNAME
  8.     $keylist = @("a","A","b","B","c","C","d","D","f","F","g","G","M","P","q","Q","R","T","V","y","Y","z","Z","2","3","4","6","8")
  9.     $pword = ""
  10.    
  11.     # Generate a simple password
  12.     1..6 | foreach $_ {
  13.         $x = Get-Random -Minimum 0 -Maximum 30
  14.         $pword += $keylist[$x]
  15.     }
  16.    
  17.     # Output samaccount and password to file.
  18.     echo "$username, $pword" | Out-File -FilePath "C:\students.csv" -Append
  19.  
  20. #Apply passwords from csv file to user accounts in AD
  21. #    "$File = import-csv -Path C:\students.csv"
  22. #foreach ($line in $File) {
  23. #$name = $line.Name
  24. #$pass = $line.Password
  25.  
  26. $secstring = ConvertTo-SecureString -String $pword -AsPlainText -Force
  27. Set-ADAccountPassword -Identity $username -NewPassword $secstring -Reset
  28. }
Add Comment
Please, Sign In to add comment