Advertisement
xrxrxr

Add-NewUsers.ps1

May 20th, 2021
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Import active directory module for running AD cmdlets
  2. Import-Module ActiveDirectory
  3.  
  4. # Store the data from NewUsersFinal.csv in the $ADUsers variable
  5. $ADUsers = Import-Csv C:\Users\LENOVO\Desktop\utilisateurs.csv -Delimiter ";"
  6.  
  7. # Define UPN
  8. $UPN = "shop.fr"
  9.  
  10. # Loop through each row containing user details in the CSV file
  11. foreach ($User in $ADUsers) {
  12.  
  13.     #Read user data from each field in each row and assign the data to a variable as below
  14.     $username = $User.Prénom.Nom
  15.     $password = $User.Prénom.Nom.1++   #ChangePasswordAtLogon
  16.     $firstname = $User.Prénom
  17.     $lastname = $User.Nom
  18.     $OU = $User.Service #This field refers to the OU the user account is to be created in
  19.     $email = $User.Prénom.Nom@$UPN
  20.     $city = $User.Agence
  21.     $jobtitle = $User.Service
  22.     $company = $User.$UPN
  23.     $department = $User.Service
  24.     # Check to see if the user already exists in AD
  25.     if (Get-ADUser -F { SamAccountName -eq $username }) {
  26.        
  27.         # If user does exist, give a warning
  28.         Write-Warning "A user account with username $username already exists in Active Directory."
  29.     }
  30.     else {
  31.  
  32.         # User does not exist then proceed to create the new user account
  33.         # Account will be created in the OU provided by the $OU variable read from the CSV file
  34.         New-ADUser `
  35.             -SamAccountName $username `
  36.             -UserPrincipalName "$username@$UPN" `
  37.             -Name "$firstname $lastname" `
  38.             -GivenName $firstname `
  39.             -Surname $lastname `
  40.             -Enabled $True `
  41.             -DisplayName "$lastname, $firstname" `
  42.             -Path $OU `
  43.             -City $city `
  44.             -Company $company `
  45.             -EmailAddress $email `
  46.             -Title $jobtitle `
  47.             -Department $department `
  48.             -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True
  49.  
  50.         # If user is created, show message.
  51.         Write-Host "The user account $username is created." -ForegroundColor Cyan
  52.     }
  53. }
  54.  
  55. Read-Host -Prompt "Press Enter to exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement