Use PowerShell to create and license users in bulk on Office 365

Published on
3,989 Points
2 Endorsements
Last Modified:
Adam the 32-bit Aardvark
Microsoft Exchange Server, Office 365 and Outlook expert with a penchant for tech forums. Looking forward to sharing skills and knowledge.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
1. Connect to your Office 365 tenant with Powershell. (Learn more...)

2. Create a CSV file with users' details. The file should contain information such as users' email address (as UPN), first name, last name etc. Example contents of a file including headers:
george.kaplan@example.onmicrosoft.com,George Kaplan,George,Kaplan,p@ssword
adam.aardvark@example.onmicrosoft.com,Adam Aardvark,Adam,Aardvark,p@ssword
modesty.blaise@example.onmicrosoft.com,Modesty Blaise,Modesty,Blaise,p@ssword

Open in new window

Users will be asked to change passwords upon the first logon.

3. To simplify the process, use variables:
$path = c:\CSV_file_name.csv
$server = remoteps:license_name

Open in new window

The "$path" variable holds information about the location of your CSV file. Replace "c:\CSV_file_name.csv" with your own file path.

The "$server" variable holds information about your current license package name. Replace it with your own license name. To learn what license you have, you can use the following command (the name will be provided in the "AccountSkuId" column):
Get-MsolAccountSku | out-gridview

Open in new window

4. With the CSV file containing users' details ready you can now import it into Office 365 and create user accounts. To do so - use the following command:
import-csv $path | foreach {New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "us"}

Open in new window

The command loops trough each CSV entry and creates respective accounts.

It also sets the "usagelocation" parameter which is required later for license assignment. In this example "US" (United States) is used - you can change it to any ISO 3166 standard code (http://en.wikipedia.org/wiki/ISO_3166-1).

5. To enable users to receive and send emails, you need to assign them their licenses. The command is similar to the previous one:
import-csv $path | foreach {set-msoluserlicense -addlicenses "$server"}

Open in new window

6. Next, check the license assignment status using the code below:
import-csv $path | Get-MSOLUser | out-gridview

Open in new window

It will display a separate window with the list of created and licensed users.

That's it - you have just created and licensed a bunch of users on Office 365.

The above procedure is especially useful when performing a migration process of your Exchange mailboxes from an on-premises server to Office 365.

Once you have all target users up and ready you can use CodeTwo Office 365 Migration (http://www.codetwo.com/office-365-migration/?sts=6377) to help you copy mailbox data over to the Cloud.
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free