?
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
Medium Priority
?
157 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 82

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
Let's recap what we learned from yesterday's Skyport Systems webinar.
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 …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

765 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