Utilizing Add-ADUser & Import-CSV Powershell Cmdlets to bulk create Active Directory accounts

We’ve all been there…your company has just taken over or brought out another company and you have been given a list of new employees to receive network accounts or another example (and my reality) it is the start of a school year and you have a herd of new year 7 students that need network accounts.

Like all good sysadmin’s, we are lazy. Why do something by hand when you can create a script or app to do the work on a large-scale. That is exactly what I have done.

You will find this time last year I tackled this same subject. I noted while my first attempt got the job done, there was plenty of additional things I wanted the script to do so I could be completely hands of when bulk creating the network accounts. Things like security group memberships or making the script handle passwords generation and User Principle Name generation automatically instead of me doing it by hand via Excel. In the name of progress I have achieved this and all in a nice annotated fashion so it’s easy for others to implement in their environment.

What do I need for this script to work?

  • Powershell V3
  • Active Directory Modules for Powershell

You can get the above a number of ways;

  1. Windows Server 2012 Domain Controller
  2. Windows 8 w/ latest Remote Server Administration Tools
  3. Windows 7 Service Pack 1, Windows Server 2008 R2 SP1, Windows Server 2008 Service Pack 2 with the WMF 3.0 update and the latest Remote Server Administration Tools
  • CSV file with three columns;
  1. Firstname
  2. Lastname
  3. Username

And here is the script, copy/paste into a text document and save with a .ps1 extension or copy/paste into your favourite Powershell IDE.

Pretty self explanatory thanks to the annotations but I will break down the key elements that you will need to fill in the blanks.

Where is the CSV file?

# Import list of Users From CSV into $Userlist

$UserList=IMPORT-CSV driveletter:\pathtofile.csv

Replace driveletter:\pathtofile.csv to the local path to the CSV file containing the user data (I recommend CSV (MS-DOS) when saving from Excel).

What is your Windows network domain name?

# Build and define Domain name

$Domain="@yourdomainhere.com"

Replace yourdomainhere.com with the fully qualified domain name of your Windows domain network.

Where is the user’s Home directory going to be?

# Build and define Home Directory path

$HDrive="\\uncpathtohomeshare\"

Replace \uncpathtohomeshare\ with the UNC path to the user’s individual file share for their home directory. Please note, there seems to be a bug that when the home directory is defined in Powershell it is not automatically created unlike when it is manually defined in the Active Directory Users and Computers MMC snap-in thus the home directories will have to be created before hand (or use %username% along with your file server UNC path while multi-selecting the properties of accounts in Active Directory Users and Computers MMC snap-in).

What organizational unit will the new accounts reside in?

# Build and define which Organizational Unit to create User inside

$OU="OU=organizationunitofyourchoice,DC=yourdomainhere,DC=com"

This one may not be applicable to all but in my case I have all my users in a well organised structure and not a free for all in the default Organizational Unit of Users. To make use of this, replace OU=organizationunitofyourchoise,DC=yourdomainhere,DC=com with the Distinguished Name path of your Organizational Unit of choice (tip: to find the DN path of an OU while in Active Directory Users and Computers snap-in go to View>Advanced Features then right-click on the OU and click Properties. In the new tab click on Attribute Editor and scroll down to Distinguished Name and copy/paste the value).

What security groups will the new accounts require?

# Add User to Security Groups

Add-ADPrincipalGroupMembership -Identity $Username -MemberOf "security group a","security group b"

Once again, this may not be applicable to all but in some cases you require all users to be a member of a  selection of security groups (note: the default Domain Users group does not need to be specified and is implied). If you are one of those people, replace security group a and security group b and security group c with the display name of your security group(s).

I hope the above explains all the minimum things you need to input for the script to work. If you are adventurous there are plenty of things you can change-up e.g. generate the username based of the firstname and lastname, how the password is generated, set the description field to Sales Staff etc… but I will leave that for you to decide.

The great thing about Powershell is how endless the possibilities are. You could use the script above to update rather than create a bulk amount of network accounts using the Set-ADUser cmdlet rather than Add-ADUser. Use your imagination and run wild!

For more information on the Add-ADUser cmdlet head to http://technet.microsoft.com/en-us/library/ee617253.aspx for a complete run down

Advertisements
James Written by:

11 Comments

  1. Joji
    13/04/2013
    Reply

    THANK YOU! I am glad I found your page! I believe you just made my (work) life easier!

    • 13/04/2013
      Reply

      My pleasure. Glad I could help you out Joji. Have a great one!

  2. sajjad Haider
    01/05/2013
    Reply

    Nice script , I have another issue , I just want to go through the list of users (email IDs) from excel or CSV file , and want to generate another Excel sheet by comparing each user properties in AD these are the columns I would like to generate in another Excel File

    I am already generating nice Excel file but it contains dump of AD , I just want to fetch exact users present in Excel sheet with the below columns.

    DisplayName
    EmployeeID
    EmployeeNumber
    Department
    Enabled
    HomeDirectory
    LastBadPasswordAttempt
    LastLogonDate
    LogonWorkstations
    City
    Manager
    MemberOf
    MobilePhone
    PasswordLastSet
    BadLogonCount
    pwdLastSet
    SamAccountName
    UserPrincipalName
    whenCreated
    whenChanged

    Get-ADUser -Filter * -Property * | Select-Object Name,Created,createTimeStamp,DistinguishedName,DisplayName,
    EmployeeID,EmployeeNumber,Department,Enabled,HomeDirectory,PasswordNeverExpires,PasswordNotRequired,LastBadPasswordAttempt,LastLogonDate,LogonWorkstations,City,Manager,MemberOf,MobilePhone,PasswordLastSet,BadLogonCount,pwdLastSet,SamAccountName,UserPrincipalName,whenCreated,whenChanged | Export-CSV Allusers.csv -NoTypeInformation -Encoding UTF8

    • 15/10/2013
      Reply

      So if I am understanding you correctly Sajjad; you have two excel documents, one has a list of email addresses (address or AD username?) and the other is to be generated with the details of said users with the columns you listed?

  3. 01/05/2013
    Reply

    So if I am understanding you correctly Sajjad; you have two excel documents, one has a list of email addresses (address or AD username?) and the other is to be generated with the details of said users with the columns you listed?

  4. Kate Swistowicz
    11/09/2013
    Reply

    How do I modify the script to include a password field? I need to create K-3 student accounts and their passwords aren’t secret. I have the passwords in plain text in the csv file.

    • 15/10/2013
      Reply

      Hi Kate,

      The script already includes a facility for password creation (check line 15). If you can add the data from your current CSV to the Import.CSV that the script will reference, then change $Password=$Person.Firstname+$Person.Lastname to $Password=$Person.yourpasswordcolumnname you should be good to go. Alternative build out your Import.CSV from your current CSV file with all the passwords in it with all the necessary columns listed.
      Does that help?

  5. mohd kamran
    23/10/2015
    Reply

    Your job is to take users.csv, and create a script that will automatically:
    Create a new user based on the name
    Set the user’s password
    Create the user’s home director

  6. mohd kamran
    23/10/2015
    Reply

    Your job is to take users.csv, and create a script that will automatically:
    Create a new user based on the name
    Set the user’s password
    Create the user’s home directory
    Can anyone help me how to do this.?

  7. Rene Aranda
    28/09/2017
    Reply

    Would you mind showing me an example of your CSV file?

    Thank you.

Helpful? Have a question on the above?