Script to powershell for create bulk users AD2008 R2

Tiago Valente 1 Reputation point
2022-08-24T08:42:52.827+00:00

hi
I need your help.
I need to create 500 users in AD windows server 2008 R2.
Required fields are:
Name (First only), Password (never expires), Group it belongs to, and path profile (access to documents that desktop on any pc in AD)

Can anyone provide script and csv template?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,533 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sean O'Brien 11 Reputation points
    2022-08-30T07:38:50.197+00:00

    Hello there,

    To create users in bulk with PowerShell, you’ll need the following:

    PowerShell module already loaded
    CSV file with information from new users
    PowerShell script to import data and create new accounts

    Below is the script to bulk import new users.

    Import active directory module for running AD cmdlets

    Import-Module activedirectory

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

    $Users = Import-csv c:\it\bulk_import.csv

    Loop through each row containing user details in the CSV file

    foreach ($User in $Users) {
    # Read user data from each field in each row
    # the username is used more often, so to prevent typing, save that in a variable
    $Username = $User.SamAccountName

    \# 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  
    
        \# create a hashtable for splatting the parameters  
        $userProps = @{  
            SamAccountName             = $User.SamAccountName                     
            Path                       = $User.path        
            GivenName                  = $User.GivenName   
            Surname                    = $User.Surname  
            Initials                   = $User.Initials  
            Name                       = $User.Name  
            DisplayName                = $User.DisplayName  
            UserPrincipalName          = $user.UserPrincipalName   
            Department                 = $User.Department  
            Description                = $User.Description  
            Office                     = $User.Office  
            OfficePhone                = $User.OfficePhone  
            EmailAddress               = $User.EmailAddress  
            StreetAddress              = $User.StreetAddress  
            POBox                      = $User.POBox  
            City                       = $User.City  
            State                      = $User.State  
            PostalCode                 = $User.PostalCode  
            Title                      = $User.Title  
            Company                    = $User.Company  
            AccountPassword            = (ConvertTo-SecureString $User.password -AsPlainText -Force)   
            Enabled                    = $true  
            ChangePasswordAtLogon      = $true  
        }   #end userprops     
    
         New-ADUser @userProps  
       \#  Write-Host "The user account $User is created." -ForegroundColor Cyan  
    

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 27,261 Reputation points Microsoft Vendor
    2022-08-30T08:02:49.21+00:00

    Hi,

    You can give a try to the script below

    $file = "C:\temp\users.csv"  
    Import-Csv -Path $file | ForEach-Object {  
        New-ADUser -Name $_.name -GivenName $_.name -AccountPassword (ConvertTo-SecureString -String $_.password -AsPlainText -Force) -PasswordNeverExpires $true -ProfilePath $_.profile -Enabled $true  
        Add-ADGroupMember -identity $_.group -Members $_.name  
    }  
    

    The fileds in the csv file are name, password, group and profile.

    236051-image.png

    Best Regards,
    Ian Xue

    -----------------------------

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments