Adding Multiple Users to Active Directory With Batch Script and Powershell

Have you ever had to enter a long list of new employees/user into Active Directory. If you have, then you know that the process is tedious and repetitive, and that’s why scripts are so useful in System Administration. There are new and probably better ways to achieve this, but for this tutorial I’m going to stick to Ms DOS batch script, and powershell. Although, the second option seems to be more efficient as powershell is still a relative new utility compare to DOS.
DOS Version. First, we assume you have a list of your users in the form of
firstname lastname username. And it is save into a csv file.
Sample file

james coburn jcoburn
tony curtis tcurtis
tina fay tfay

using MS DOS

@echo off
echo importing users into Active Directory
for /F "tokens=1-3" %%a in (C:\users.csv) do (
	dsadd user "cn=%%c,cn=users,dc=contoso,dc=com" -fn %%a -ln %%b -upn %%c@contoso.com -mustchpwd yes -pwd Password1
)

PowerShell Version. The script takes into account that you already have a csv file in the following format.
Create-Bulk-AD-Users-From-CSV-using-Powershell-4
Then, you can use this simple script for the previous csv file.

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";
}
Share This!

Leave a Reply

Your email address will not be published.