I am importing a few users to AD through a CSV file. I am trying to get 2 things working.

  1. Manager name - I cannot get this to populate - What exactly will I need the header to be for the manager name in the CSV?

  2. How can I add a different password for each user and enable the account. I was able to get this work by adding the Enable $True and also adding a generic password with convert to secure string only thing is this adds the password for all the users.

  3. I havent tried yet - BUt how do I import these users into a specific OU?

5 Spice ups
  1. Whatever you have put as the title in your CSV you will need to reference in your script.

  2. If you want to add a different password then you need to specify it in your script.

  3. Specify the OU in your CSV and then reference it in your script.

I did this sort of thing a couple of months back and it took a lot of ‘Googling’, testings and essentially playing until I got it how I wanted it. I also used Windows Powershell ISE to build my script, which I found very, very useful. I’m sure there are other Powershell gurus on here who can probably simplify it down but that is basically how I went about it.

csv will contain

fisrtname, lastname,password,manager
jack,sparrow,P@ssw0rd,Mr. Manager

Remove email field if you do not want set ou

<#
#**************************************************BULK AD User Creation By Allenage.com*****************************************#
 version 0.1
 Just add fisrtname, lastname in the csv and place on C:\ as users.csv 

 Other changes

 $OU="CN=users, DC=Domain,DC=COM"   change OU path as desired.

Here are examples of samaccountname or username comment out rest which does'nt suit your organistation and keep the required one.

$SAM = $user.FirstName.Substring(0,1) + $user.LastName #example John snow will be Jsnow
    #$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

#>
Import-module activedirectory
$Users=Import-csv c:\users.csv
$a=1;
$b=1;
$failedUsers = @()
$usersAlreadyExist =@()
$successUsers = @()
$VerbosePreference = "Continue"
$LogFolder = "$env:userprofile\desktop\logs"
ForEach($User in $Users)
{
 $User.FirstName = $User.FirstName.substring(0,1).toupper()+$User.FirstName.substring(1).tolower()
   $User.LastName = $User.LastName.substring(0,1).toupper()+$User.LastName.substring(1).tolower()
   $FullName = $User.FirstName + " " + $User.LastName
   $SAM = $user.FirstName.Substring(0,1) + $user.LastName #example John snow will be Jsnow
    #$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()
   $UPN = $SAM + "$dnsroot"
   $OU="CN=users, DC=Domain,DC=COM"
   $email=$Sam + "$dnsroot"

try {
    if (!(get-aduser -Filter {samaccountname -eq "$SAM"})){
        New-ADUser -Name $FullName -AccountPassword (ConvertTo-SecureString $user.password -AsPlainText -force) -GivenName $User.FirstName  -Path $OU -SamAccountName $SAM -Surname $User.LastName  -UserPrincipalName $UPN  -Manager $User.manager -EmailAddress $Email -Enabled $TRUE
        Write-Verbose "[PASS] Created $FullName"
        $successUsers += $FullName
    }
   
}
catch {
    Write-Warning "[ERROR]Can't create user [$($FullName)] : $_"
    $failedUsers += $FullName
}
}
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

The header of the column isn’t important, so long as you use the same string in your script. The value for the column has to be one of the following:

Distinguished Name
Example: CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com

GUID (objectGUID)
Example: 599c3d2e-f72d-4d20-8a88-030d99495f20

Security Identifier (objectSid)
Example: S-1-5-21-3165297888-301567370-576410423-1103

SAM Account Name (sAMAccountName)
Example: saradavis

A quick bit of code to generate a random string:

( (1..10) | ForEach-Object { [char]( (33..126) | Get-Random ) } ) -join ''

You can use the -Path parameter of New-ADUser.

4 Spice ups

in a very simple language

1> Manager name - I cannot get this to populate - What exactly will I need the header to be for the manager name in the CSV?

2> Different passwords for each users

If csv contains

fisrtname, lastname,password,manager
jack,sparrow,P@ssw0rd,Mr. Manager
Tom,hanks,some@122password,Vinod Kumar

Powershell

new-ADUser  -AccountPassword (ConvertTo-SecureString $user.password -AsPlainText -force)  -Path $OU -SamAccountName $SAM -Surname $User.LastName  -UserPrincipalName $UPN  -Manager $User.manager  -Enabled $TRUE

2> havent tried yet - BUt how do I import these users into a specific OU

define OU somewhere

 $OU="OU=My ou, DC=Domain,DC=COM" 

powershell script already looking for OU here

-Path $OU

When I run this script - it gives me an error. Cannot bind argument to parameter ‘String’ because it is null.

The snippet that Jiten posted above isn’t meant to be ran on its own. It’s an excerpt from the script in his first post.

Got it - I ran the full script and now I get the following

WARNING: [ERROR]Can’t create user [Joe Blow] : The object name has bad syntax

Do not put any spaces on csv

it wll look like

FirstName,lastname,password,manager
jack,sparrow,P@ssw0rd,MR manager
jollie,cabera,P@ssw0rd,Mr manager
Joe,Blow,P@ssw0rd,MR manager

im easily able to create

VERBOSE: [PASS] Created Jack Sparrow
VERBOSE: [PASS] Created Jollie  Cabera
VERBOSE: [PASS] Created Joe Blow
VERBOSE: Writing logs
VERBOSE: Performing operation "Output to File" on Target "C:\Users\Administrator\desktop\logs\FailedUsers.log".
VERBOSE: Performing operation "Output to File" on Target "C:\Users\Administrator\desktop\logs\successUsers.log".

My CSV had no spaces.

I ended up getting these imported and just added the manager name manually and will change passwords as the accounts get distributed.

Thanks for the help!