Many times we
come across a scenario where we need to create bulk of AD user accounts. But
who wants to spend a whole bunch of time for creating users? So we at Deskperts
went ahead and made a PowerShell Script that will create users from a CSV file.
Hope you find it useful!!!!
We have tested the below script in Windows Server 2008 R2 AD infrastructure.
We have created a test OU as Sales in our testlab.com with no users.
Note: - Give the csv file name same as 1st line in the script.
Copy both files at same location on AD.
AD Users will be created as below.
We have tested the below script in Windows Server 2008 R2 AD infrastructure.
We have created a test OU as Sales in our testlab.com with no users.
Below is the Powershell script in text format to create bulk
users in AD.
Note:- Save the file with .PS1 format (Powershell)
The Script data
is below, copy the below text into Notepad, and save it as Original.ps1
Import-Csv .\Original.csv | foreach-object {
$UserPrincipalName = $_.SamAccountName + "@TESTLAB.COM"
New-ADUser -SamAccountName $_.SamAccountName -UserPrincipalName $UserPrincipalName -Givenname $_.FirstName -Name $_.FirstName -DisplayName $_.DisplayName -SurName $_.LastName -Department $_.Department -EmailAddress $_.EmailAddress -EmployeeID $_.EmployeeID -Path $_.Path -AccountPassword (ConvertTo-SecureString "pass@123" -AsPlainText -force) -Enabled $True -ChangePasswordAtLogon $True -PassThru }
Create
the csv file with user properties to be created in AD as below.
Note: - Give the csv file name same as 1st line in the script.
Copy both files at same location on AD.
To run the script
on AD:
- Run Active Directory Module for PowerShell as Administrator
- Change to the location where you have
placed the Script and the CSV file.
- Now run the script. Type “.\CreateAccount.ps1”
AD Users will be created as below.
I attempted to use this code to create 100 users, but I received the following error. Have you run into it and do you know a solution?
ReplyDeleteThanks
The term 'New-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\dbcot\users.ps1:4 char:11
+ New-ADUser <<<< -SamAccountName $_.SamAccountName -UserPrincipalName $UserPrincipalName -Givenname $_.FirstName -Nam
e $_.FirstName -SurName $_.LastName -Path $_.Path -AccountPassword $_.AccountPassword -Enabled $True -ChangePasswordAt
Logon $True -PassThru }
+ CategoryInfo : ObjectNotFound: (New-ADUser:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Are you using the Active Directory Module for Windows PowerShell??? If not you have to import it with Import-Module cmdlet.
DeleteHi David,
ReplyDeleteRequest you to provide detail of the infra setup, like DC servers etc.
David, if you are using Windows Server 2008 DC, you may need to import cmdlets for Active Directory, before using any AD Powershell cmdlets.
ReplyDeleteThe cmdlets for Active directory:
ReplyDeleteimport-module ActiveDirectory
You may be able to put this in to the .ps1 file specified above.
Is there a way to add a password value and import it too?
ReplyDeleteHow will the PowerShell and CSV file should be?
Thanks in advance!!!
I attempted to use this code to create 50 users, but I received the following error. Have you run into it and do you know a solution?
ReplyDeleteThanks
New-ADUser : Cannot validate argument on parameter 'Name'. The argument is null
or empty. Supply an argument that is not null or empty and then try the comman
d again.
At C:\temp\Add-NewUsersDesarrollo.ps1:4 char:21
+ New-ADUser -Name <<<< $_.DisplayName -SamAccountName $_.SamAccountName -
GivenName $_.FirstName -Surname $_.LastName -DisplayName $_.DisplayName -Descri
ption $_.Description -EmployeeID $_.EmployeeID -UserPrincipalName $userPrinc -A
ccountPassword (ConvertTo-SecureString -AsPlainText "TBcBviV6" -Force) -Enabled
$true -path "OU=PruebasVS,DC=desarrollocolpatria,DC=com"}
+ CategoryInfo : InvalidData: (:) [New-ADUser], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
eDirectory.Management.Commands.NewADUser
have a look here for advanced active directory objects(users,computer,group,contact,OU,etc) creation/modification/deletion
ReplyDeletehttp://www.adsysnet.com/asn-active-directory-manager-bulk-objects-creation.aspx