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 ADUserscreation.csv which contains set of New AD Users to create with the attributes Name,samAccountName and etc.
Powershell Script to Create Bulk AD Users from CSV file
1) Consider the CSV file ADUserscreation.csv which contains set of New AD Users to create with the attributes Name,samAccountName and etc.
2) Copy the below Powershell script and paste in Notepad file.
3) Change the ADUserscreation.csv file path with your own csv file path.
4) Change the domain name test.com into your own domain name
5) SaveAs the Notepad file with the extension .ps1 like USERCREATION.ps1
$objOU=[ADSI]“LDAP://OU=Internal,DC=test,DC=com”
$dataSource=import-csv “ADUserCreation.csv”
foreach($dataRecord in $datasource) {
$cn=$dataRecord.FirstName + ” ” + $dataRecord.LastName
$email=$dataRecord.Email
$description=$dataRecord.EmpID
$address=$dataRecord.UserAddress
$company=$dataRecord.CompanyName
$department=$dataRecord.UserDepartment
$physicalDeliveryOfficeName=$dataRecord.OfficeDetail
$title=$dataRecord.JobTitle
$telephoneNumber=$dataRecord.UserTelephoneNumber
$mobile=$dataRecord.UserMobile
$sAMAccountName=$dataRecord.SAM
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$givenName + ” ” + $sn
$userPrincipalName=$sAMAccountName + “@test.com”
$objUser=$objOU.Create(“user”,”CN=”+$cn)
$objUser.Put(“sAMAccountName”,$sAMAccountName)
$objUser.Put(“userPrincipalName”,$userPrincipalName)
$objUser.Put(“displayName”,$displayName)
$objUser.Put(“givenName”,$givenName)
$objUser.Put(“sn”,$sn)
$objUser.Put("mail",$email)
$objUser.Put("description",$description)
$objUser.Put("mobile",$mobile)
$objUser.Put("streetAddress",$address)
$objUser.Put("company",$company)
$objUser.Put("department",$department)
$objUser.Put("physicalDeliveryOfficeName",$physicalDeliveryOfficeName)
$objUser.Put("title",$title)
$objUser.Put("telephoneNumber",$telephoneNumber)
$objUser.SetInfo()
$objUser.SetPassword(“Test@123”)
$objUser.pwdLastSet = 0
$objUser.psbase.InvokeSet(“AccountDisabled”,$false)
$objUser.SetInfo()
}
6) Now run the USERCREATION.ps1 file in Powershell to create Bulk Active Directory users from CSV file.
PSC:\Scripts> .\USERCREATION.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.
No comments:
Post a Comment