Jump to content

Recommended Posts

Posted

Hi,

 

Does anyone have a script to create new users in AD from a CSV that they would be willing to share?

 

Just taken a new role and they tell me that they dont have any automated system in place for user provisioning.

 

Thanks in advance

  • 2 weeks later...
Posted

# Import active directory module for running AD cmdlets

Import-Module ActiveDirectory

 

# Store the data from NewUsersFinal.csv in the $ADUsers variable

$ADUsers = Import-Csv C:\temp\NewUsersFinal.csv

 

# Define UPN

$UPN = "school.region.sch.uk"

 

# Loop through each row containing user details in the CSV file

foreach ($User in $ADUsers) {

 

#Read user data from each field in each row and assign the data to a variable as below

$username = $User.username

$password = $User.password

$firstname = $User.firstname

$lastname = $User.lastname

#$initials = $User.initials

$OU = $User.ou #This field refers to the OU the user account is to be created in

$email = $User.email

#$streetaddress = $User.streetaddress

#$city = $User.city

#$zipcode = $User.zipcode

#$state = $User.state

#$country = $User.country

#$telephone = $User.telephone

$jobtitle = $User.jobtitle

#$company = $User.company

$department = $User.department

$description = $user.description

$office = $user.office

$o = $User.o

 

# Check to see if the user already exists in AD

if (Get-ADUser -F { SamAccountName -eq $username }) {

 

# If user does exist, give a warning

Write-Warning "A user account with username $username already exists in Active Directory."

}

else {

 

# User does not exist then proceed to create the new user account

# Account will be created in the OU provided by the $OU variable read from the CSV file

New-ADUser `

-SamAccountName $username `

-UserPrincipalName "$username@$UPN" `

-Name "$firstname $lastname" `

-GivenName $firstname `

-Surname $lastname `

-Initials $initials `

-Enabled $True `

-DisplayName "$firstname $lastname" `

-Path $OU `

-City $city `

-PostalCode $zipcode `

-Country $country `

-Company $company `

-State $state `

-StreetAddress $streetaddress `

-OfficePhone $telephone `

-EmailAddress $email `

-Title $jobtitle `

-Department $department `

-Office $office `

-Description $description `

-OtherAttributes @{'o'=$User.o} `

-AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $False

 

# If user is created, show message.

Write-Host "The user account $username is created." -ForegroundColor Cyan

}

}

 

Read-Host -Prompt "Press Enter to exit"

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...