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
153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 81

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

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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 …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

687 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