PowerShell Script Request: Windows AD user creation from Script.

PowerShell Script Request: Windows AD user creation from Script.
Windows 2016 AD
Input as CSV  file: Please provide a template for mandatory information.
LVL 1
Murty MAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alex GreenProject Systems EngineerCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shaun VermaakTechnical Specialist/DeveloperCommented:
This is the script and sample CSV from the link above

Script
###########################################################
# AUTHOR  : Kelly Bush
# DATE    : 1/16/2014  
# COMMENT : Creates Active Directory users and populates
#           different attributes based on import_users.csv
# Based on: PowerShell: Create Active Directory Users Based On Excel Input 
###########################################################
Import-Module ActiveDirectory
$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath  = $path + "\import_users.csv"
# Define variables
$log      = $path + "\created_ActiveDirectory_users.log"
$date     = Get-Date
$i        = 0

Function createActiveDirectoryUsers
{

"Created the following Active Directory users (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append

  Import-CSV $newpath | ForEach-Object { 
    $samAccount = $_.SamAccountName
    Try   { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$samAccount)" }
    Catch { }
    If(!$exists)
    {
      $i++
      # Set all variables according to the table names in the Excel 
      # sheet / import CSV. The names can differ in every project, but 
      # if the names change, make sure to change it below as well.
      $setpass = ConvertTo-SecureString -AsPlainText $_.Password -force
      New-ADUser -Name $_.DisplayName -SamAccountName $_.SamAccountName -GivenName $_.GivenName -Initials $_.Initials `
      -Surname $_.SN -DisplayName $_.DisplayName -Office $_.OfficeName `
      -Description $_.Description -EmailAddress $_.eMail `
      -StreetAddress $_.StreetAddress -City $_.L `
      -PostalCode $_.PostalCode -Country $_.CO -UserPrincipalName $_.UPN `
      -Company $_.Company -Department $_.Department -EmployeeID $_.ID `
      -OfficePhone $_.Phone -AccountPassword $setpass -Enabled $true -Path $_.OU  
  
      $output  = $i.ToString() + ") Name: " + $_.CN + "  sAMAccountName: " 
      $output += $sam + "  Pass: " + $_.Password
      $output | Out-File $log -append
    }
    Else
    {
      "SKIPPED - USER ALREADY EXISTS OR ERROR: " + $_.CN | Out-File $log -append
    }
  }
  "----------------------------------------" + "`n" | Out-File $log -append
}
# Run function
createActiveDirectoryUsers
#End Script

Open in new window


CSV
OU,CN,GivenName,Initials,SN,DisplayName,SamAccountName,OfficeName,Description,eMail,StreetAddress,L,PostalCode,CO,UPN,Company,Department,ID,Title,Phone,Manager,Password
"OU=All Users,DC=Lab,DC=local",s.database.windows.001,s.database.windows.001,,,s.database.windows.001,s.database.win.001,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.001@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.002,s.database.windows.002,,,s.database.windows.002,s.database.win.002,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.002@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.003,s.database.windows.003,,,s.database.windows.003,s.database.win.003,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.003@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.004,s.database.windows.004,,,s.database.windows.004,s.database.win.004,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.004@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.005,s.database.windows.005,,,s.database.windows.005,s.database.win.005,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.005@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.006,s.database.windows.006,,,s.database.windows.006,s.database.win.006,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.006@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.007,s.database.windows.007,,,s.database.windows.007,s.database.win.007,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.007@lab.local,,,,,,,X8TmDRrr
"OU=All Users,DC=Lab,DC=local",s.database.windows.008,s.database.windows.008,,,s.database.windows.008,s.database.win.008,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.008@lab.local,,,,,,,X8TmDRrr
"OU=Service Accounts,DC=Lab,DC=local",s.database.windows.009,s.database.windows.009,,,s.database.windows.009,s.database.win.009,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.009@lab.local,,,,,,,X8TmDRrr
"OU=Service Accounts,DC=Lab,DC=local",s.database.windows.010,s.database.windows.010,,,s.database.windows.010,s.database.win.010,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.010@lab.local,,,,,,,X8TmDRrr
"OU=Service Accounts,DC=Lab,DC=local",s.database.windows.011,s.database.windows.011,,,s.database.windows.011,s.database.win.011,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.011@lab.local,,,,,,,X8TmDRrr
"OU=Service Accounts,DC=Lab,DC=local",s.database.windows.012,s.database.windows.012,,,s.database.windows.012,s.database.win.012,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.012@lab.local,,,,,,,X8TmDRrr
"OU=Service Accounts,DC=Lab,DC=local",s.database.windows.013,s.database.windows.013,,,s.database.windows.013,s.database.win.013,Database Team - New York office,Requested for Appteam by John Doe,USA.DBSupport@xyz.com,,,,,s.database.windows.013@lab.local,,,,,,,X8TmDRrr

Open in new window

1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.