Facebook
From Rude Bat, 1 Week ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 39
  1. # Import the ActiveDirectory module
  2. Import-Module ActiveDirectory
  3.  
  4. # Define the path to the CSV file
  5. $csvPath = "C:\path\to\csv\file.csv"
  6.  
  7. # Define the OU where the new users will be created
  8. $ouPath = "OU=Employees,DC=example,DC=com"
  9.  
  10. # Define the password length and complexity
  11. $passwordLength = 10
  12. $passwordComplexity = [System.Security.Cryptography.PasswordDeriveBytes]::Create("P@ssw0rd!", 0, 0).GetBytes(32)
  13.  
  14. # Read the CSV file
  15. $users = Import-Csv $csvPath
  16.  
  17. # Loop through each user and create an AD account
  18. foreach ($user in $users) {
  19.     # Generate a random password
  20.     $password = [System.Web.Security.Membership]::GeneratePassword($passwordLength, 2)
  21.  
  22.     # Set the AD account properties
  23.     $adUser = @{
  24.         GivenName = $user.'First name'
  25.         Surname = $user.'Last name'
  26.         DisplayName = "$($user.'First name') $($user.'Last name')"
  27.         Name = "$($user.'First name') $($user.'Last name')"
  28.         UserPrincipalName = $user.Email
  29.         SamAccountName = $user.Email.Split("@")[0]
  30.         AccountPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
  31.         Enabled = $true
  32.         Path = $ouPath
  33.     }
  34.  
  35.     # Create the AD account
  36.     New-ADUser @adUser
  37.  
  38.     # Print the username and password
  39.     Write-Host "Username: $($adUser.SamAccountName)"
  40.     Write-Host "Password: $password"
  41. }