PowerShell Script Request: Windows AD user creation from Script.

Murty M
Murty M used Ask the Experts™
on
PowerShell Script Request: Windows AD user creation from Script.
Windows 2016 AD
Input as CSV  file: Please provide a template for mandatory information.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial