Saturday 6 June 2020

Create Bulk User in Active Directory Using PowerShell


Create Bulk User in Active Directory Using PowerShell




First Need to Check Syntax What would be needed while Creating User.















Setup Csv File Format. (userlist.csv)








How to Find Ou Path

Ou Path is the DistinguishedName Attribute, to Find this open up Active Directory Users and

Computers and Browse to the OU you want to import to, then right click and select properties
Then select attribute editor.


































Paste the Path into the OU Column on the Csv file. 


Setup PowerShell Script

Set  your CSV file Path.

#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\Users\Administrator\Desktop\userlist.csv



Change the User Principle Name






Please update as per your Domain Name

   -UserPrincipalName "$Username@abc.local"


   Save and run the Script.





# Import active directory module for running AD cmdlets
Import-Module activedirectory
 
#Store the data from ADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\Users\Administrator\Desktop\userlist.csv

#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
       $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
    $Password = $User.Password
    $logonworkstations = $User.userWorkstations


       #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 exist 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@abc.local" `
            -Name "$Firstname" `
            -GivenName $Firstname `
            -Surname $Lastname `
            -Enabled $True `
            -DisplayName "$Firstname" `
            -Path $OU `
            -City $city `
            -Company $company `
            -State $state `
            -StreetAddress $streetaddress `
            -OfficePhone $telephone `
            -EmailAddress $email `
            -Title $jobtitle `
            -Department $department `
            -LogonWorkstations $logonworkstations `
            -Description $description `
            -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -CannotChangePassword $True `
             -PasswordNeverExpires $True
              #-ChangePasswordAtLogon $True
           
       }
}

No comments:

Post a Comment