Adding users to AD via a CSV file

As mentioned several times in this book, https://www.spiceworks.com/ has a busy PowerShell support forum (accessible at https://community.spiceworks.com/programming/powershell). A frequently asked (and answered) question is: How do I add multiple users using an input file? This recipe does just that.

Start with a CSV file containing details of the users you are going to add. This recipe uses a CSV file and adds the users into the AD.

Getting ready

This recipe assumes you have a domain setup and that you have created the IT OU. You did this in earlier recipes in this chapter. This recipe also requires a CSV file of users to add. You can create a CSV file like so:

$CSVDATA = @'
Firstname, Initials, LastName, UserPrincipalName, Alias, Description, Password
S,K,Masterly, skm, Sylvester, Data Team, Christmas42
C,B Smith, CBS, Claire, Claire, Receptionist, Christmas42
Billy-Bob, Joe-Bob, Bob, BBJB, BBJB, One of the Bob's, Christmas42
Malcolm, DoWrite, Duelittle, Malcolm, Malcolm, Mr Danger, Christmas42
'@
$CSVDATA | Out-File -FilePath C:\Foo\Users.Csv

How to do it...

  1. Import a CSV file containing the details of the users you wish to add to AD:
    $Users = Import-CSV -Path C:\Foo\Users.Csv | 
      Sort-Object -Property Alias
    $Users | Sort-Object -Property alias |FT
  2. Add the users using the CSV:
    ForEach ($User in $Users) {
    #    Create a hash table of properties to set on created user
    $Prop = @{}
    #    Fill in values
    $Prop.GivenName         = $User.Firstname
    $Prop.Initials          = $User.Initials
    $Prop.Surname           = $User.Lastname
    $Prop.UserPrincipalName = $User.UserPrincipalName+"@reskit.org"
    $Prop.Displayname       = $User.FirstName.trim() + " " +
    $user.LastName.Trim()
    $Prop.Description       = $User.Description
    $Prop.Name              = $User.Alias
    $PW = ConvertTo-SecureString -AsPlainText $user.password -Force
    $Prop.AccountPassword   = $PW
    #    To be safe!
    $Prop.ChangePasswordAtLogon = $true
    #    Now create the user
    New-ADUser @Prop -Path 'OU=IT,DC=Reskit,DC=ORG' -Enabled:$true
    #   Finally, display user created
    "Created $($Prop.Displayname)"
    }

How it works...

In step 1, you import the CSV file from C:\Foo\Users.Csv, which was noted in the Getting ready section of this recipe. Importing the CSV file generates no output.

In step 2, you iterate through the users in the CSV. For each user in the file, you first generate a hash table ($Prop) which you pass to the New-ADUser cmdlet to add the user to AD.

After you add each user, the recipe displays a message noting that the new user is now added to the AD. If you run the entire recipe as a single script, saved as Add-UsersToAD.ps1, and use the C:\foo\Users.Csv file created at the start of this recipe, then the output looks like this:

There's more...

The basic approach of adding a user based on data in a CSV is straightforward. There are many variations on this approach that you can take depending on your circumstances.

You can expand the data included in the CSV file to populate more user properties for each AD user. For example, you could include a cell phone number, office address, and much more. Another variation is extending the CSV file and including one or more security groups that should have the user added.