Friday, December 6, 2019

Create Bulk Users in Active Directory via Power Shell

Create Bulk Users in Active Directory via Power Shell


The procedure of bulk user creation in Active directory mainly included 3 Steps.

1. Create A CSV File with Users Information.
2. Create a Power Shell Script
3. Run Power Shell Script.

1. Create A CSV File with Users Information:

Create a CSV file with Users information and ensure the CSV file have at least "Firstname" , "Lastname", "Username", "Password" and "OU" information.


Sample CSV and Script Download Link

2. Crate a Power Shell Script:


Create a Power Shell Script with below codes. Edit only "CSV file location" and "UserPrincipalName" parameter which is highlighted below.

Keep CSV file in a location which is specified in script.

###############Script Start#######################

# Importing AD cmdlets
Import-Module activedirectory
  
#Set $ADUsers variable from file Bulkusers.csv
$ADUsers = Import-csv C:\scripts\bulkusers.csv

#Looping for each user with user information 
foreach ($User in $ADUsers)
{
#Assign data to variable by reading the filed

$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou
    $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
    $Password = $User.Password


#Checking User ID already Exist or not
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#give a warning If user does exist
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#If ID not exit, will proceed with user creation

        #Account will be created in OU specified in CSV
New-ADUser `
            -SamAccountName $Username `
            -UserPrincipalName "$Username@vdilab.com" `
            -Name "$Firstname $Lastname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -Enabled $True `
            -DisplayName "$Lastname, $Firstname" `
            -Path $OU `
            -City $city `
            -Company $company `
            -State $state `
            -StreetAddress $streetaddress `
            -OfficePhone $telephone `
            -EmailAddress $email `
            -Title $jobtitle `
            -Department $department `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
            
}
}

###############Script End #######################


3. Execute Power Shell Script.

Pre-Requisites:

a) Need to run this script from Machine where RSAT Tools installed.
b) The User should have rights to add Users in AD.

Steps:

1. Open "Windows Power Shell ISE" with "Run As Administrator"
2. Click File > Open and browse to the power Shell script, and click open.
3. Once script is load, click "Green Run button" to execute script.


4. Script has been executed and users are created as per csv file.


Sample CSV and Power Shell Script Download URL





1 comment:

Enter Comments...