SHARE
TWEET

powershell add users

a guest Jan 15th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Enter a path to your import CSV file
  2. $ADUsers = Get-Content C:\Users\Administrator\Desktop\file.csv
  3.  
  4. for ($i = 0; $i -lt $ADUsers.Length; $i++)
  5. {
  6.     Write-Output ([string]$ADUsers[$i])
  7.     $firstname = ([string]$ADUsers[$i]).Split(' ')[1]
  8.    
  9.     $lastname = ([string]$ADUsers[$i]).Split(' ')[2]
  10.     $fullname = "$firstname $lastname"
  11.     $logonname = "$firstname.$lastname"
  12.     $OU = "CN=Users,DC=cosci,DC=be"
  13.     $domain = $env:userdnsdomain
  14.     $Password = "School123"
  15.     $Pass = ConvertTo-SecureString $Password -AsPlainText -Force
  16.     $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Pass)
  17.     if (Get-ADUser -F {SamAccountName -eq $logonname})
  18.         {
  19.                 #If user does exist, output a warning message
  20.                 Write-Warning "A user account $Username has already exist in Active Directory."
  21.         }
  22.     else
  23.     {
  24.         Write-Host "Creating Active Directory user account now" -ForegroundColor:Green
  25.  
  26.         #Creating user account with the information you inputted.
  27.         New-ADUser -Name $fullname -GivenName $firstname -Surname $lastname -DisplayName $fullname -SamAccountName $logonname -UserPrincipalName $logonname@$Domain -AccountPassword $pass -Enabled $true -Path $OU  -Confirm:$false
  28.    
  29.     }  
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top