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
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 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.
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
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.
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.
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.
Hi Morgan,
Do we have additional commands if I will work via remote desktop using may APAC domain account.
Thanks a lot..
Erik
Quick question, have you used Excel or Access to create the CSV file?
I just used notepad, but viewed in Excel to create screenshot that you are seeing in my article
Hey , how can I set the password for each user in the CSV?
thx.
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
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.
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.
You can read this post to export ad users to csv file : https://www.morgantechspace.com/2014/11/Export-AD-Users-to-CSV-using-Powershell.html
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?
sorry for late reply, you can have it like this.
Could you please give some idea, how to create bulk AD user account disable.
please checkout this post for your need. https://www.morgantechspace.com/2014/11/Disable-Bulk-AD-Users-from-CSV-using-Powershell.html
I need add first name and Last name and display Name