Create Bulk AD Users from CSV using Powershell Script

Creating Bulk AD Users in Active Directory is one of the important task for every Administrator either for testing or for set of actual new employees. Normally you can create new AD user using ADUC console. But it is time consuming job if you want create multiple ad users at the same time. To overcome this every administrator should rely on any of the script technology like VBScript and Powershell. In this article. I am going write and explain Powershell Script to Create Bulk AD Users from CSV.

 

Before proceed, please ensure that the Active Directory module for Windows Powershell is installed or not in your machine. It will be installed by default in Domain Controller. In client machines, you need to install it through Remote Server Administration Tools.
Use below command to check Active Directory module is installed or not:

Get-Module -Listavailable
Create Multiple AD Users from CSV using Powershell Script

If you are newbie to Powershell, don’t forget to set your Execution Policy to unrestricted or you might get an error when you try run the script. Use the below command to set your Execution Policy:

Set-ExecutionPolicy Unrestricted
Powershell Script to Create Bulk Active Directory Users from CSV

Powershell Script to Create Bulk AD Users from CSV file

   1. Consider the CSV file NewUsers.csv which contains set of New AD Users to create with the attributes Name, samAccountName and ParentOU.

Create Bulk AD Users from CSV file using Powershell Script

Note: The value of ParentOU should be enclosed with double quote (). like “OU=TestOU,DC=TestDomain,DC=Local” since it has the special character comma (,). because in csv file the comma (,) is the key character to split column headers. (Ex file: Download NewUsers.csv).

   2. Copy the below Powershell script and paste in Notepad file.
   3. Change the NewUsers.csv file path with your own csv file path.
   4. Change the domain name TestDomain.local into your own domain name
   5. SaveAs the Notepad file with the extension .ps1 like Create-BulkADUsers-CSV.ps1

Click to download Powershell script as file Download Create-BulkADUsers-CSV.ps1

Import-Module ActiveDirectory
Import-Csv "C:\Scripts\NewUsers.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@TestDomain.Local"
New-ADUser -Name $_.Name `
 -Path $_."ParentOU" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true
Add-ADGroupMember "Domain Admins" $_."samAccountName";
}

   6. Now run the Create-BulkADUsers-CSV.ps1 file in Powershell to create Bulk Active Directory users from CSV file.

PS C:\Scripts> .Create-BulkADUsers-CSV.ps1
Create Bulk Active Directory Users from CSV Powershell Script

Note: I have placed script file in the location C:\Scripts, if you placed in any other location, you can navigate to that path using CD path command (like cd “C:\Downloads”).

   7. Now you can check the newly Created AD Users though ADUC console.

Powershell Script to Create Bulk AD Users from CSV file

 

Add more AD Attributes to New User:

Here, we have Created Bulk AD Users from CSV with only three attributes Name, samAccountName and ParentOU by CSV input. If you want to give more attributes from CSV input, you can add that attributes into csv file and change the above Powershell script accordingly.

Example: if you want to add EmailAddress to new user, your csv file should be like below file.

Create Bulk AD Users from CSV Powershell Script

Change the Powershell script like this:

Import-Module ActiveDirectory
Import-Csv "C:\Scripts\NewUsers.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@TestDomain.Local"
New-ADUser -Name $_.Name `
 -Path $_."ParentOU" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "MyPassword123" -AsPlainText -Force) `
 -ChangePasswordAtLogon $true  `
 -Enabled $true `
 -EmailAddress $_."EmailAddress"
Add-ADGroupMember "Domain Admins" $_."samAccountName";
}

Refer this technet article http://technet.microsoft.com/en-us/library/ee617253.aspx to Create Bulk AD Users with more AD attributes.

Next: Export AD users to CVS using Powershell


Advertisement

16 thoughts on “Create Bulk AD Users from CSV using Powershell Script”

  1. When I run this code it has an error
    New-ADUser : Directory object not found
    At F:CreateADUserCreate-BulkADUsers-CSV.ps1:4 char:1
    + New-ADUser -Name $_."Name" `
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (CN=TestUser,OU=…verDev,DC=Local:String) [New-ADUser], ADIdentityNotFo
    undException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
    icrosoft.ActiveDirectory.Management.Commands.NewADUser

    Can you help me fix it

    Reply
  2. Thanks for this tutorial , i have freeipa server, and export all the user to text file ,i need to export this text file to windows server 2012 and i try that you mention but didn't work for me , please can you help me with that.
    BR.

    Reply
  3. Thanks for this tutorial .
    I have freeipa and export all user to text file , and i need to migrate this file to windows server 2012, i try that you mention but didn't work for me please can you help me with that,
    and you can see the text file result

    —————
    2 users matched
    —————
    User login: admin
    Last name: Administrator
    Home directory: /home/admin
    Login shell: /bin/bash
    UID: 1023400000
    GID: 1023400000
    Account disabled: False
    Password: True
    Kerberos keys available: True

    User login: booboo
    First name: boobs
    Last name: boobs
    Home directory: /home/booboo
    Login shell: /bin/bash
    Email address: [email protected]
    UID: 1023400003
    GID: 1023400003
    Account disabled: False
    Password: True
    Kerberos keys available: True
    —————————-
    Number of entries returned 2
    —————————-
    BR.

    Reply
  4. Is it possible to keep it as:

    ForEach-Object {
    $userPrincinpal = $_."samAccountName" + "@domain.com" instead of testdomain.local?

    When I'm trying to create my bulk users?

    Reply

Leave a Comment