Skip to content
March 2, 2014 / Don

Creating Users from a Spreadsheet with New-ADUser

Working in my lab, I needed a script to create some AD users. Here is what I came up with.

 

 

##################### Start of script ########################
#########################################################
#########################################################
#########################################################
### Written by Don Wilwol
###
### 3-2014
### atthedatacenter.wordpress.com
###
###Creates users from a CSV file
###Just add firstname, lastname and (important) header in the input file
###
###It logs to the log file path
###It sets the SamAccountName to first initial lastname
##It sets the UPN to the SamAccountName@domain
###It sets a primary Email address as the UPN
###It sets the SIP address as the UPN
###It surpresses the errors in the shell
###to see the errors rem out $ErrorActionPreference = "SilentlyContinue"
###Creation of a user with appended suffix is logged
###The script defaults to a password of P@ssw0rd" but the line can be changed
###Change the line with this on for "first initial, last initial, !12345"
# -AccountPassword(Convertto-Securestring -AsPlaintext ($_.Firstname.SubString(0,1) + $_.lastname.SubString(0,1) + "!12345") -Force)`

$date = (get-date).toString(‘yyyy-MM-dd’)

 

##Set input file path
$CSVPath ="c:\users.csv"

##Set YOUR domain name
$DomainN = "student.Source.lab"

##Set the OU Path you would like the users created
$Path = "OU=TestUsers,DC=Source,DC=Lab"

### Set the description for this group of users
$Description = "Student Account for Some Year, Maybe 2019"

###Set the log file path
$LogFile = "C:\$date-CreateUserlogfile.txt"

#########################################################
#########################################################
#########################################################

$ErrorActionPreference = "SilentlyContinue"

 

import-csv $CSVPath | foreach {

$SamAccountname = ($_.Firstname.SubString(0,1) + $_.lastname)
If(get-ADUser $SamAccountname) {$SamAccountname = $SamAccountname + "2";write-output "**Secondary SamAccountName**  $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "3";write-output "**3rd SamAccountName** $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "4";write-output "**4th SamAccountName** $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "5";write-output "**5th SamAccountName** $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "6";write-output "**6th SamAccountName** $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "7";write-output "**7th SamAccountName** $SamAccountName" >> $LogFile}
if(get-ADUser $SamAccountname) {$SamAccountname = $_.Firstname.SubString(0,1) + $_.lastname + "8";write-output "**8th SamAccountName** $SamAccountName" >> $LogFile}

 

$error.clear()
$UPN = $SamAccountname + "@$DomainN"
$Dname = ($_.Firstname + " " + $_.lastname)
new-aduser -Path $Path `
-Name $Dname -GivenName $_.Firstname`
-Surname $_.Lastname`
-AccountPassword(Convertto-Securestring -AsPlaintext "P@ssw0rd" -Force)`
-enabled $true `
-EmailAddress $UPN `
-SamAccountname $SamAccountname  `
-Description $Description `
-DisplayName $Dname `
-ChangePasswordAtLogon $True
write-host   ("Creating User (" + $SamAccountname + ")  " + $_.Firstname + " " + $_.lastname)
write-output "" >> $LogFile
write-output ("Creating User (" + $SamAccountname + ")  " + $_.Firstname + " " + $_.lastname) $error >> $LogFile
write-output "" >> $LogFile 

Set-Aduser -Identity $SamAccountname -Add @{userPrincipalName = $UPN;`
proxyAddresses = "SMTP:$UPN","SIP;$UPN";`
}
}

##################### End of script ########################

 

The spreadsheet is a very simple format.

firstname,lastname

Joe,Somebody

Harry,Helper

Fred,Flinstone

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: