fbpx Program: Import PowerShell Users in Bulk to Active Directory | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Program: Import PowerShell Users in Bulk to Active Directory

When importing several users into Active Directory, it quickly becomes tiresome to do it by hand (or even to script the addition of each user onebyone). To solve this problem, we can put all our data into a CSV, and then do a bulk import from the information in the CSV.

Example 232 supports this in a flexible way. You provide a container to hold the user accounts and a CSV that holds the account information. For each row in the CSV, the script creates a user from the data in that row. The only mandatory column is a CN column to define the common name of the user. Any other columns, if present, represent other Active Directory attributes you want to define for that user.

Example 232. ImportADUser.ps1

############################################################################## ## ## ImportAdUser.ps1 ## ## Create users in Active Directory from the content of a CSV. ## ## For example: ## $container = "LDAP://localhost:389/ou=West,ou=Sales,dc=Fabrikam,dc=COM" ## ImportADUser.ps1 $container .\users.csv ## ## In the user CSV, One column must be named "CN" for the user name.

Example 232. ImportADUser.ps1 (continued)

## All other columns represent properties in Active Directory for that user. ## ## For example: ## CN,userPrincipalName,displayName,manager ## MyerKen,Ken.Myer@fabrikam.com,Ken Myer, ## DoeJane,Jane.Doe@fabrikam.com,Jane Doe,"CN=MyerKen,OU=West,OU=Sales,DC=..." ## SmithRobin,Robin.Smith@fabrikam.com,Robin Smith,"CN=MyerKen,OU=West,OU=..." ## ##############################################################################

param( $container = $(throw "Please specify a container (such as " +

"LDAP://localhost:389/ou=West,ou=Sales,dc=Fabrikam,dc=COM)"), $csvPath = $(throw "Please specify the path to the users CSV") )

## Bind to the container $userContainer = [adsi] $container

## Ensure that the container was valid if(not $userContainer.Name) {

WriteError "Could not connect to $container" return }

## Load the CSV $users = @(ImportCsv $csvPath) if($users.Count eq 0) {

return }

## Go through each user from the CSV foreach($user in $users) {

## Pull out the name, and create that user $username = $user.CN $newUser = $userContainer.Create("User", "CN=$username")

## Go through each of the properties from the CSV, and set its value ## on the user foreach($property in $user.PsObject.Properties) {

## Skip the property if it was the CN property that sets the ## user name if($property.Name eq "CN") {

continue }

## Ensure they specified a value for the property

Example 232. ImportADUser.ps1 (continued)

if(not $property.Value) { continue }

## Set the value of the property $newUser.Put($property.Name, $property.Value) }

## Finalize the information in Active Directory $newUser.SetInfo() }

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say