Bulk Creation of Accounts Using PowerShell

powershell

With the introduction of the new server and domain, we had to come up with a new procedure for the bulk creation of the accounts.   No longer is it as simple as using a .bat script and creating the account, creating the directory, setting the ownership, and then adding them to a group.   Now, we need to also set the home directory and add them into an organization unit.   The following documents the PowerShell script, the input .csv file, and the procedure for the creation of the accounts.


The Location of the Files

The files are located on TPAW-01 in the folder c:\adminBin\NewUsers.   In there you will find the PowerShell script saved as .ps1 and a .csv file that has the data to create the user accounts.

The PowerShell Command

The following is the PowerShell Command:

Stored as Create-Bulk-From-CSV.ps1

Import-Module ActiveDirectory

Import-Csv "C:\adminBin\NewUsers\NewUsers.csv" | ForEach-Object {
$userPrincinpal = $_."samAccountName" + "@tigerden.org";

# Creates the new account
New-ADUser -Name $_."FullName" ` -Path $_."ParentOU" `
-GivenName $_."GivenName" ` -DisplayName $_."FullName" ` -Surname $_."Surname" ` -HomeDrive $_."HomeDrive" ` -HomeDirectory $_."HomeDirectory" ` -SAMAccountName  $_."samAccountName" ` -UserPrincipalName  $userPrincinpal ` -AccountPassword (ConvertTo-SecureString $_."Password" -AsPlainText -Force) ` -ChangePasswordAtLogon $true ` -Enabled $true; # Adds the user to the group Add-ADGroupMember $_."DefaultGroup" $_."samAccountName"; # Creates the users's home directory and sets the proper security New-Item -ItemType directory -Path $_."HomeDirectory"|Out-Null; #set acl to folder $Acl = Get-Acl $_."HomeDirectory" $Ar = New-Object system.security.accesscontrol.filesystemaccessrule($_."samAccountName","FullControl","Allow") $Acl.SetAccessRule($Ar) Set-Acl $_."HomeDirectory" $Acl }

This command will perform the following:

  1. Reads in the data from the .csv file
  2. Creates the New Account
  3. Puts the account within a default group
  4. Creates the Home Directory for the user (Z:\)
  5. Sets the protection on the new folder to the user’s account

The .CSV file

The .csv data file will contain the following columns, if you wish to see and/or use a template, look at the newUsers.csv file within the same folder of the PowerShell script.   You will have to save your input file “newUsers.csv”.

  • Full Name (First and Last)
  • Given Name (First Name)
  • Surname (Last Name)
  • Account Name (Username)
  • Password (Default Password)
  • Parent OU (Organization Unit to place the account in)
  • Default Group (The default group)
  • Home Drive (In most cases Z:\)
  • Home Directory (This is the path using the domain share folder, i.e.  \\tigerden\userdata\z\[group for the account]

Some things to consider

  1. This new script does not have much error checking built into it.   It simply takes the data and attempts to create the accounts.
  2. With the new domain, it does not matter which server you run the script on; however, at the moment it is only on TPAW-01.   It does not matter that the users directories are located on TPAW-02, or TPAW-03 since it will create the account folders and the user account within the Active Domain and not a specific server.
  3. At the time of writing this document, it has not be tested on a large number of account; however, I would suggest only doing small groups at a time in case you do receive some errors.
  4. There is a file called NewUsers.csv_SAMPLE in the account creation folder that can be used to create your input file.  DO NOT MODIFY the sample file, since it is configured perfectly and has been tested with the creation of user accounts.

To create new “Bulk” accounts

Use the following procedure to create bulk accounts:

  1. Update and/or create a NewUsers.csv using the same columns as listed above.   You need to make sure the they first row has the exact same field names that is in the sample .csv file.
  2. Place the NewUsers.csv in the same folder as the PowerShell Script, i.e. c:\AdminBin\NewUsers\ on TPAW-01.
  3. Run PowerShell as administrator
  4. In PowerShell, change the directory to the c:\AdminBin\NewUsers folder.
  5. Execute the PowerShell command by typing in .\Create-Bulk-From-CSV.ps1

As you execute the command, you should not see any red error messages being displayed.   If you do, you need to look at each of the users and determine what had failed during the creation of the account.

If you did not receive any error messages, spot check a couple of accounts and make sure that they were created correctly.

Leave a Reply

You must be logged in to post a comment.