Solved

Adding/importing 50 users to a new AD at once using CVS file or Excell file 2012 server

Posted on 2014-12-01
1
129 Views
Last Modified: 2015-01-12
I am rebuilding a domain and need to add 50 users to the domain. I rather not do this one by one. I need to do this within the next hour or so.(  I hope to hear back by then :-)    ) .   The usernames are going to be firstInitialLastname and all the passwords can be temp passwords. I am doing this on a 2012 server standard.
0
Comment
Question by:netcomp
1 Comment
 
LVL 78

Accepted Solution

by:
David Johnson, CD, MVP earned 500 total points
ID: 40475143
powershell create-adusers.ps1
###########################################################
# AUTHOR  : Marius / Hican - http://www.hican.nl - @hicannl 
# modified by DavidJohnson @ve3ofa
# DATE    : 26-04-2012 
# COMMENT : This script creates new Active Directory users
#           including different kind of properties based
#           on an input_create_ad_users.csv.
# 
###########################################################
Import-Module ActiveDirectory
# Get current directory and set import file in variable
$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath  = $path + "\import_create_ad_users.csv"
# Define variables
$log      = $path + "\create_ad_users.log"
$yourdomain = "Example"
$YourCompany = "Contoso"
$date     = Get-Date
$i        = 0
# Change this to the location you want the users to be created in your AD
$location = "CN=NewYork,$DC=corp,DC=$yourdomain,DC=com"
# FUNCTIONS
Function createUsers
{
  "Created following users (on " + $date + "): " | Out-File $log -append
  "--------------------------------------------" | Out-File $log -append
  $newpath
  $users = Import-CSV $newpath

ForEach  ($user in $users) 
    {
$CommonName = $user.FirstName + "."
if ($User.MiddleName.length -ge 0){
     $CommonName = $CommonName  + $user.MiddleName
     }
$CommonName = $CommonName + "." + $user.LastName
$CommonName

# A check for the country, because those were full names and need 
    # to be landcodes in order for AD to accept them. I used Netherlands 
    # as example
    If($User.CO -eq "Canada")
    {
      $User.CO = "CA"
    }
    If($User.CO -eq "United States")
    {
      $User.CO = "US"
    }
    If($User.CO -eq "France")
    {
      $User.CO = "FR"
    }
    # Replace dots / points (.) in names, because AD will error when a 
    # name ends with a dot (and it looks cleaner as well)
    
    # Create sAMAccountName according to this 'naming convention':
    # <FirstLetterInitials><FirstFourLettersLastName> for example
    # hhica
    $sam = $User.FirstName + $user.middlename + $User.LastName
    $sam = $sam.toLower()
    $sam
    
    Try   { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
    Catch { }
    If(!$exists)
    {
      $i++
      New-ADUser -SamAccountName $sam -GivenName $user.FirstName 
      New-ADUser -City $user.City -Company $yourCompany -Department $user.department
     



-Intials $user.initials -
      # 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 $User.Password -force
      New-ADUser $sam -GivenName $User.GivenName -Initials $User.Initials `
      -Surname $User.LastName -DisplayName $User.DisplayName -Office "New York" `
      -Description $User.Description -EmailAddress $User.Mail `
      -StreetAddress $User.StreetAddress -City $User.City `
      -PostalCode $User.PostalCode -Country $User.CO -UserPrincipalName $User.UPN `
      -Company $User.Company  -EmployeeID $User.EmployeeID `
      -Title $User.Title -OfficePhone $User.Phone -AccountPassword $setpass
 
      # Set an ExtensionAttribute
      $dn  = (Get-ADUser $sam).DistinguishedName
      $ext = [ADSI]"LDAP://$dn"
      If ($User.ExtensionAttribute1 -ne "" -And $User.ExtensionAttribute1 -ne $Null)
      {
        $ext.Put("extensionAttribute1", $User.ExtensionAttribute1)
        $ext.SetInfo()
      }
 
      # Move the user to the OU you set above. If you don't want to
      # move the user(s) and just create them in the global Users
      # OU, comment the string below
  #    Move-ADObject -Identity $dn -TargetPath $location
 
      # Rename the object to a good looking name (otherwise you see
      # the 'ugly' shortened sAMAccountNames as a name in AD. This 
      # can't be set right away (as sAMAccountName) due to the 20
      # character restriction
      $newdn = (Get-ADUser $sam).DistinguishedName
#      Rename-ADObject -Identity $newdn -NewName $CommonName
 
      $output  = $i.ToString() + ") Name: " + $CommonName + "  sAMAccountName: " 
      $output += $sam + "  Pass: " + $User.Password
      $output | Out-File $log -append
    }
    Else
    {
      "SKIPPED - ALREADY EXISTS OR ERROR: " + $CommonName | Out-File $log -append
    }
  }
  "----------------------------------------" + "`n" | Out-File $log -append
}


# RUN SCRIPT
createUsers
#Finished

Open in new window

0

Join & Write a Comment

Suggested Solutions

Introduction You may have a need to setup a group of users to allow local administrative access on workstations.  In a domain environment this can easily be achieved with Restricted Groups and Group Policies. This article will demonstrate how to…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now