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
133 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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Super Scope, DHCP 5 52
How to prioritize LOGONSERVER for clients? 1 40
edit user account 1 30
ADFS Passive Request = "There are no registered protocol handlers" 2 47
Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
[b]Ok so now I will show you how to add a user name to the description at login. [/b] First connect to your DC (Domain Controller / Active Directory Server) SET PERMISSIONS FOR SCRIPT TO UPDATE COMPUTER DESCRIPTION TO USERNAME 1. Open Active …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

920 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