LOGO

Create Multiple Users in Server 2008 with PowerShell - A Guide

December 16, 2009
Create Multiple Users in Server 2008 with PowerShell - A Guide

Automating User Account Creation in Active Directory

While creating users individually via the AD Users and Computers snap-in is straightforward, administrators often encounter scenarios requiring the simultaneous creation of accounts for numerous individuals. Fortunately, this process doesn't need to be unduly time-consuming. We will outline a method to streamline this task, having already prepared the necessary groundwork for you.

Preparing the User Data

A list of new employees has been received from the Human Resources Department, conveniently provided in an Excel format. Ensure your Excel file adheres to the same structure, utilizing "First Name" and "Last Name" as the column headings.

The initial step involves saving the Excel file as a .csv file. This is accomplished by clicking the Office Button and selecting "Save As".

how-to-create-multiple-users-in-server-2008-with-powershell-1.jpg

Name the file "users.csv" and, crucially, select "CSV (Comma delimited)" from the "Save as type" dropdown menu. Then, click "Save".

how-to-create-multiple-users-in-server-2008-with-powershell-2.jpg

Creating the PowerShell Script

Next, a new text document will be created on the server where the user creation will take place.

how-to-create-multiple-users-in-server-2008-with-powershell-3.jpg

The following PowerShell script should then be copied into this new text document:

$objOU=[ADSI]"LDAP://OU=People,DC=sysadmingeek,DC=com"

$dataSource=import-csv "users.csv"

foreach($dataRecord in $datasource) {

$cn=$dataRecord.FirstName + " " + $dataRecord.LastName

$sAMAccountName=$dataRecord.FirstName + "." + $dataRecord.LastName

$givenName=$dataRecord.FirstName

$sn=$dataRecord.LastName

$sAMAccountName=$sAMAccountName.ToLower()

$displayName=$sn + ", " + $givenName

$userPrincipalName=$sAMAccountName + "@sysadmingeek.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.SetInfo()

$objUser.SetPassword("P@assw0rd")

$objUser.psbase.InvokeSet("AccountDisabled",$false)

$objUser.SetInfo()

}

It is vital to update the first line of the script with the correct information for your Active Directory domain and the Organizational Unit (OU) where the users will be created. Also, modify the @sysadmingeek.com line to reflect your domain name.

how-to-create-multiple-users-in-server-2008-with-powershell-4.jpg

To save the file as a PowerShell script, change the "Save as type:" to "All Files (*)" and name it "PSusersScript.ps1".

how-to-create-multiple-users-in-server-2008-with-powershell-5.jpg

Executing the Script

PowerShell must be configured to allow script execution. Launch PowerShell either through the taskbar shortcut or by typing "PowerShell" in the quick search box.

how-to-create-multiple-users-in-server-2008-with-powershell-6.jpg

To enable script execution, execute the following command:

set-executionpolicy remotesigned

When prompted, type "Y" and press Enter to confirm.

how-to-create-multiple-users-in-server-2008-with-powershell-7.jpg

Place both the "users.csv" and "PSusersScript.ps1" files in the same folder. Since the PowerShell prompt defaults to the root user folder, and assuming you are logged in as an Administrator, the C:UsersAdministrator folder is a suitable location. With both files present, right-click on "PSusersScript.ps1" and select "Run with PowerShell".

how-to-create-multiple-users-in-server-2008-with-powershell-8.jpg

Verification and Future Use

Verify the successful creation of the new users by checking within AD Users and Computers.

how-to-create-multiple-users-in-server-2008-with-powershell-9.jpg

The script creates users in the "lastname.firstname" format, though this can be readily adjusted to meet your specific requirements. Once the script is configured, future user creation simply involves placing the updated user list in the designated folder and executing the PowerShell script. This provides a simple and efficient solution for bulk user account creation.

#Server 2008#PowerShell#user creation#multiple users#scripting#Active Directory