Avatar of Murty M
Murty M
Flag for India asked on

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.
Powershell

Avatar of undefined
Last Comment
Shaun Vermaak

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Alex

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Shaun Vermaak

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

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck