hello all i have a new client that has a ton of users… they werent using active directory. and im going crazy typing these users in and i was like… it would be great to import all of these users!

is there a way to script a csv file to import these headers into active directory on server 2012 r2?

Username
first name
last name
temp password
full name
OU they belong infor group policy
Branch name
Email

please advise. thanks

6 Spice ups

It can be done with a powershell script

1 Spice up

Sure thing.

You might have to format it a bit but should work just fine.

What have you tried where are you stuck?

If you post code, please use the ‘Insert Code’ button. Please and thank you!

please check the best script ever

https://community.spiceworks.com/scripts/show/3682-bulk-create-active-directory-users-powershell-with-logs-less-rows-in-csv

Its simple and easy,

This article should help: How to Bulk Import AD Users from CSV | Adaxes Blog How to Bulk Import AD Users from CSV | Adaxes Blog

1 Spice up

AS per your DM you need

username
firstname
lastname
password
Full name
OU
email

add those as a header in CSV and run

If (!(Get-module ActiveDirectory )) {
  Import-Module ActiveDirectory
  Clear-Host
  }

$Users=Import-csv c:\users.csv
$a=1;
$b=1;
$failedUsers = @()
$successUsers = @()
$VerbosePreference = "Continue"
$ErrorActionPreference='stop'
$LogFolder = "$env:userprofile\desktop\logs"

 ForEach($User in $Users)
   {
   $FirstName = $User.FirstName.substring(0,1).toupper()+$User.FirstName.substring(1).tolower()
   $LastName  = $User.LastName.substring(0,1).toupper()+$User.LastName.substring(1).tolower()

   $FullName = $User.FirstName + " " + $User.LastName

   $SAM = $user.username
   <#
   $Sam=$User.FirstName+$User.LastName --> example john snow will be Johnsnow
   $Sam=$User.FirstName --> example john snow will be John
   $Sam= $User.firstName + "." + $User.lastName  --> example john snow will be John.snow
   $Sam=$user.Lastname+$user.Firstname.Substring(0,1))  --> example john snow will be sjohn
   #>

   $dnsroot = '@' + (Get-ADDomain).dnsroot

   $SAM=$sam.tolower()

   # To set Diffreent Passwords for each User add header Password on CSV and change 'P@ssw0rd@123' to $user.passsword
   $Password = (ConvertTo-SecureString -AsPlainText $user.password -Force)

   
   $UPN = $SAM + "$dnsroot" # change "$dnsroot to custom domain if you want, by default it will take from DNS ROOT"

   $OU=$user.OU
   $email=$user.email
Try {
    if (!(get-aduser -Filter {samaccountname -eq "$SAM"})){
     $Parameters = @{
    'SamAccountName'        = $Sam
    'UserPrincipalName'     = $UPN 
    'Name'                  = $Fullname
    'EmailAddress'          = $Email 
    'GivenName'             = $FirstName 
    'Surname'               = $Lastname  
    'AccountPassword'       = $password 
    'ChangePasswordAtLogon' = $true # Set False if you do not want user to change password at next logon.
    'Enabled'               = $true 
    'Path'                  = $OU
    'PasswordNeverExpires'  = $False # Set True if Password should expire as set on GPO.
}

New-ADUser @Parameters
     Write-Verbose "[PASS] Created $FullName "
     $successUsers += $FullName + "," +$SAM
    }
   
}
Catch {
    Write-Warning "[ERROR]Can't create user [$($FullName)] : $_"
    $failedUsers += $FullName + "," +$SAM + "," +$_
}
}
if ( !(test-path $LogFolder)) {
    Write-Verbose "Folder [$($LogFolder)] does not exist, creating"
    new-item $LogFolder -type directory -Force 
}

Write-verbose "Writing logs"
$failedUsers   |ForEach-Object {"$($b).) $($_)"; $b++} | out-file -FilePath  $LogFolder\FailedUsers.log -Force -Verbose
$successUsers | ForEach-Object {"$($a).) $($_)"; $a++} | out-file -FilePath  $LogFolder\successUsers.log -Force -Verbose

$su=(Get-Content "$LogFolder\successUsers.log").count
$fu=(Get-Content "$LogFolder\FailedUsers.log").count

Write-Host "$fu Users Creation Failed and " -NoNewline -ForegroundColor red
Write-Host "$su Users Successfully Created "  -NoNewline -ForegroundColor green
Write-Host "--> Launching LogsFolder have a Look and review." -ForegroundColor Magenta
Start-Sleep -Seconds 5
Invoke-Item $LogFolder
1 Spice up