Pages

Tuesday, April 11, 2017

Using Windows Power Shell to Create User Accounts in Bulk

Introduction

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.


Model Solutions


Prerequisites
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.        Launch Excel and setup the basic spreadsheet structure. Create the CSV file msita.csv with the attributes Name, samAccountName and ParentOU.


           2.        Copy the below Powershell script and paste in Notepad file:

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

         3.        Change the msita.csv file path with your own csv file path.
Import-Csv "C:\msita.csv" | ForEach-Object {
         4.        Change the domain name msita.local into your own domain name.
$userPrincinpal = $_."samAccountName" + "@msita.local"
         5.        SaveAs the Notepad file with the extension .ps1 like MSITA_BulkADUsers_CSV.ps1


        6.        Create HR Organizational Unit on the Active Directory Users and Computers.


         7.        Now run the MSITA_BulkADUsers_CSV.ps1 file in Powershell to create Bulk Active Directory users from CSV file.


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

         8.        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:\msita.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@msita.local"
New-ADUser -Name $_.Name `
 -Path $_."ParentOU" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "abc123###" -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.


 ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
                                                    ► Download this video, lesson for FREE
                                                    ► PDF link: http://adf.ly/1n40UF
                                                    ► Alternate link: ...
      ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

No comments:

Post a Comment