This content is part of the Essential Guide: The essential admin's guide to Office 365 PowerShell
Manage Learn to apply best practices and optimize your operations.

Add Active Directory user accounts in bulk with PowerShell

Administrators can use PowerShell scripts to automate the creation of Active Directory user accounts for new employees.

Do you have lots of new employees to add to Active Directory? There's a better way than copying and pasting into...

the graphical user interface: using PowerShell scripts instead.

One of the most prominent responsibilities of Exchange administrators is creating Active Directory user accounts and mailboxes. All dynamic organizations hire new employees; a typical scenario consists of human resources (HR) giving the Active Directory or Exchange administrator an Excel spreadsheet or CSV file that was exported out of an HR application from which they must create Exchange user accounts. It is then up to the administrator to convert each of those CSV rows into an Active Directory user account and an Exchange mailbox.

PowerShell scripts let the admin read each of the rows in that CSV and then pass all of those fields in the proper arrangement directly to Exchange -- in this example we're using Exchange Online -- to create Active Directory user accounts and Exchange mailboxes all in one shot. Once the PowerShell script is built, it's simply a matter of passing the CSV file you'd like to use and running the script -- quite a time-saver.

The first task is to point PowerShell's built-in cmdlet Import-CSV at a CSV file. It will read each row into memory and save each row as an object with all the properties you'd expect.

The CSV file from HR probably will look like the example in Figure 1, with just the first and last name of a group of new employees. We can construct additional fields for the Active Directory accounts on the fly with PowerShell.

Employees to add to Office 365.
Figure 1. A typical employee CSV file from your organization might look like this. These are the employees you'll add to Office 365.

Bring in the CSV rows in memory by assigning them to the $users variable.

$users = Import-CSV –Path C:\Users.csv

Next, make a connection to Exchange Online by creating a PowerShell Remoting session and using implicit remoting to make all of the functions to manage Exchange Online available.

First, get a pscredential object using the Get-Credential cmdlet.

$UserCredential = Get-Credential

Next, build a new PowerShell Remoting session with PowerShell Remoting passing the pscredential object to the New-PSSession cmdlet along with the ConnectionUri parameter and authentication method.

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic –AllowRedirection

Confirm the session has been created properly by checking $Session to see if the session is available and the state is opened (see Figure 2).

$Session for PowerShell
Figure 2. $Session shows whether PowerShell Remoting successfully established a session.

Next, use the Import-PSSession cmdlet to pull all of the cmdlets from that session into your local session through implicit remoting.

$module = Import-PSSession –Session $Session

I'm assigning the output of Import-PSSession to a $module variable because Import-PSSession outputs a module that holds all of the cmdlets. You can run Get-Command after to confirm that the cmdlets are available. Various cmdlets will scroll by.

Get-Command –Module $module.Name

Now you're ready to pass each of the CSV rows to the New-Mailbox cmdlet, which will create the Active Directory user account and the Exchange mailbox for each employee.

Need to create users in bulk in Office 365 rather than Exchange Online? The process is similar with a few key differences.

The hardest part is creating the various parameters that New-Mailbox needs to make the user and mailbox. Below is a snippet of code to use as a template. In this example, I create each employee's account with an Alias and Name of first initial/lastname. I'm setting the DisplayName to first name/last name, assigning a MicrosoftOnlineServicesID as first initial/lastname @ and assigning it a password of [email protected]$$w0rd. I wouldn't recommend putting the password in clear text like this. If you're adding accounts to Exchange in production, look into using the Export-CliXml cmdlet to save a pscredential object to the filesystem and read it that way.

$domain = ''

Foreach ($user in $users) {

    $alias = '{0}{1}' –f $user.FirstName.SubString(0,1),$user.LastName

    $mailboxParams = @{

    'Alias' = $alias

    'Name' = $alias

    'FirstName' = $user.FirstName

    'LastName' = $user.LastName

    'DisplayName' = "$($user.FirstName) $($user.LastName)"

    'MicrosoftOnlineServicesID' = "[email protected]$domain"

    'Password' = (ConvertTo-SecureString -String '[email protected]' -AsPlainText -Force)

    'ResetPasswordOnNextLogon' = $true


New-Mailbox @mailboxParams


You will probably get a warning message for each user about assigning the license to the user. After you create a new mailbox, you must go to the Office 365 Admin Center and assign the mailbox a license, or it will be disabled after the grace period.

You should now have new user accounts and mailboxes built for all employees from that CSV file.

Author's note: This article works with Exchange Online in the cloud. You can use the same cmdlet New-Mailbox to build on-premises Exchange mailboxes, but the syntax is a little different.

Next Steps

Use PowerShell to manage Exchange Online

How to manage Office 365 from Active Directory

Authenticate users with Azure AD PowerShell

Dig Deeper on Exchange Server setup and troubleshooting