다음을 통해 공유


Creating Batch Users in Active Directory using PowerShell

Overview

Implemented on Windows Server 2008 R2 the PowerShell module simplifies and administrative or repetitive tasks made by administrators.

Implemented on the module, New-ADUser is used to created new users and objects on Active Directory Domain Services. This cmdlet can be used alone or using pipes as part of a script to help on repetitive tasks.  

Before using the cmdlets is necessary to import the administrative modules on PowerShell. Run the cmdlet: 

*Import-Module ActiveDirectory *

In this article all commands were run on a domain controller running Windows Server 2012 R2.

**Creating A User **

To create a user with the minimal information required, just set the Name parameter and UserPrincipalName. Running the cmdlet

New-ADUser -Path 'OU=Home Users,dc=home,dc=intranet' -Name User17.U17 -UserPrincipalName User17.U17@home.intranet

The result is a user named User17 created OU Home Users. By default the user is created disabled and no password set. 

   

This user has been created without information about name or a displayname , we can add more information using the parameters of the cmdlet. To create a user with the information of name and surname populated just run. 

New-ADUser -Path 'OU=Home Users,dc=home,dc=intranet' -Name User18.U18 -UserPrincipalName User18.U18@home.intranet -Surname U18 -GivenName User18 -DisplayName "User18 U18"

In the properties of the new user can verify the configured name

Creating Batch Users

The cmdlet also accepts lists of pipes from variable or other cmdlets, that helps expands its functionality. In this case we can use the cmdlet to create a list of users using a CSV input file. 

Excel 2013 was used to create and format the list of users. 

Creating a basic CSV File

The first batch of users will be created with the minimum necessary, the CSV file to create users in this following format

The end result will be users with  DisplayName, UPN and Name properties set.

**Creating Users **

To create the users above first import the list to a session variable in PowerShell. Copy the file to server, and execute cmdlet 

$u01 = Import-Csv Usuarios.csv

Then run New-ADUser using the variable, 

$u | ForEach-Object { New-ADUser -Path 'OU=Home Users,dc=home,dc=intranet' -Name $_.Name -UserPrincipalName $_.UPN -DisplayName $_.DN  }

The user should be create 

**Creating a CSV With Password and Email **

This cmdlet can also be used to enable users after creating and configuring the password. 

The user's password was generated directly in Excel with the formula:

=CHAR(RANDBETWEEN(65;90))&CHAR(RANDBETWEEN(97;122))&CHAR(RANDBETWEEN(97;122))
&CHAR(RANDBETWEEN(65;90))&RANDBETWEEN(1000;9999)&CHAR(RANDBETWEEN(42;43))

The final file with the email fields, names and passwords set should look like the following

**Creating Users with Passwords and Email **

Copy the new file to the server and import it into a new variable

$u2 = Import-Csv Usuario2.csv

To create this list of users execute the following:

$u2 | ForEach-Object { New-ADUser -Path 'OU=Home Users,dc=home,dc=intranet' -Name $_.Name -Surname $_.SurN -GivenName $_.FirstN -UserPrincipalName $_.UPN -DisplayName $_.DN -EmailAddress $_.SMTP -AccountPassword (ConvertTo-SecureString -AsPlainText $_.Pass -Force) -Enabled $True}

The new list of enabled user is created and the password defined 

   

Reference

Other Languages