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

Create 10, 100 or more Office 365 accounts with PowerShell

Administrators who need to create accounts for new employees can save time by using PowerShell scripts to read and import data from a CSV file straight into Office 365.

Why repeat the same task dozens or hundreds of times in the Office 365 Admin Center?

You probably provision new Office 365 accounts on a regular basis if you manage the email platform for your organization. This is possible via the Office 365 Admin Center portal or via PowerShell. If you need to create one or two user accounts, use the Admin Center portal. However, if you find yourself building dozens or hundreds of accounts on a regular basis, learn to script the process with PowerShell.

The Office 365 administrator can use PowerShell to read some kind of data source, such as a CSV file. We can then read each row of this CSV file representing various employees and pass each one to the New-MsolUser cmdlet to seamlessly build one, 10 or 1,000 Office 365 accounts at one time.

You'll need the Microsoft Online Services Sign-In Assistant and the Microsoft Azure Active Directory (AD) PowerShell Module installed. These are both Windows Installer MSI files. Download and install them, accepting the defaults. You will only have to install these once.

Once you install the prerequisite software, bring up a PowerShell session and run Connect-MsolService. This will prompt you for your Office 365 credentials and connect you.

Add new employees to Office 365

We can now begin to process those employees in the CSV file. PowerShell has a built-in cmdlet called Import-CSV to read each row of the CSV file into memory, and save each row as an object with all the properties you'd expect; you simply point Import-CSV to your list of new employees.

The CSV file that you'll get from human resources may simply have the first name and last name of a group of new employees (see the figure) from which you'll make new Office 365 user accounts. We need more fields than first and last name, fields which we can construct on the fly with PowerShell scripts.

Employees to add to Office 365.
Figure. 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, assigning them to the $users variable.

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

Now that each employee's first and last name are stored in the $users variable, loop through each of those employees and run the New-MsolUser cmdlet for each one.

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

Because the CSV just contains the employee's first and last name, construct a few parameters for the New-MsolUser cmdlet. These are the UserPrincipalName and the DisplayName that will be associated with the Office 365 accounts. The UserPrincipalName needs to be in the format of first initial/last name @ my domain. Build this by assigning a variable $firstInitLastName that is the employee's first initial/last name and combining that with the domain name. Also, the display name needs to be first name/last name with a space in between, so build that via a string as well.

$domain = 'adamtheautomator.com'

foreach ($user in $users) {

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

    $newUserParams = @{

         'FirstName' = $user.FirstName

         'LastName' = $user.LastName

         'UserPrincipalName' = "$firstInitLastName@$domain"

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

         'Password' = 'P@ssw0rd12'

    }

    New-MsolUser @newUserParams

}

Once this reads each of the users in the $users variable, you should now have new Office 365 accounts created for each employee that was in your original CSV file.

If you'd rather not store the password in your script, you can store passwords encrypted on the file system.

Author's Note: this article is based on using version 0.98 of the Azure AD module.

Next Steps

The worst email problems Office 365 admins face

Admins' must-know apps for Office 365 and Exchange

A plan to merge accounts on Office 365

This was last published in February 2016

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What Office 365 tasks do you use PowerShell scripts to automate?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchEnterpriseDesktop

SearchVirtualDesktop

Close