How to add and license users in bulk on Office 365

PowerShell becomes a tool of choice for a growing number of Office 365 administrators. The main reason is that they can leverage all benefits of the scripting environment in repetitive, everyday tasks.

Even mass creation of users and licenses assignment is not a problem – below you’ll find a step by step guide describing the process.

Connect to Office 365 with PowerShell

Follow this short tutorial to learn how to remotely connect to your Office 365 with PowerShell.

Create a CSV file

Next step is to create a list of all users you want to create in CSV format. It will contain all necessary details, such as email addresses, first and last names, passwords etc. Below you can find an example CSV file syntax.

UserPrincipalName,DisplayName,FirstName,LastName,password
[email protected],Clark Kent,Clark,Kent,Asik12
[email protected],Bruce Wayne,Bruce,Wayne,Poiy32
[email protected],Peter Parker,Peter,Parker,Rujk99

Make sure to replace the “remoteps.onmicrosoft.com” domain with your own.

Environment variables

In this example two variables are set:

$path = c:\new_users.csv 
$server = remoteps:enterprisepack

$path contains the path to your CSV file. Replace “c:\new_users.csv” with your own path.

$server contains your license package name. Use the following command to check what license you have (it is listed in the AccountSkuId column):

Get-MsolAccountSku | out-gridview

Create users

The command below loops trough each line of your CSV file and creates an account based on details provided.

import-csv $path | foreach {

New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname 
$_.displayname -firstname $_.firstname -lastname $_.lastname -password 
$_.Password -usagelocation "us"

}

The -usagelocation parameter is required for license assignment. You can change it to the standard country code of your choice.

Licenses assignment

Without the assigned license the Office 365 account cannot send/receive emails. The command below works similarly to the previous one – it works through each line of your CSV file and enables the license for each newly created user.

import-csv $path | foreach { 
set-msoluserlicense -addlicenses "$server" 
}

To make sure that licenses were assigned correctly run the code below.

import-csv $path | Get-MSOLUser | out-gridview

The process is complete. All users from the CSV file list are now present in your Office 365 environment, with licenses assigned and ready to send and receive messages.

All described steps are also attached below as a single script. You can save it as a PS1 file. Additionally it includes a small code that pops up a file picking window which helps to set the $path variable.

#CSV file picker module start
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} 

#CSV file picker module end

#Variable that holds CSV file location from file picker
$path = Get-FileName -initialDirectory "c:\" 

#Window with list of available 365 licenses and their names
Get-MsolAccountSku | out-gridview 

#Input window where you provide the license package's name 
$server = read-host 'Provide licensename (AccountSkuId)' 

#CSV import command and mailbox creation loop
import-csv $path | foreach {
New-Msoluser -userPrincipalName $_.UserPrincipalName -displayname $_.displayname -firstname $_.firstname -lastname $_.lastname -password $_.Password -usagelocation "us" | set-msoluserlicense -addlicenses "$server"
}

#Result report on licenses assigned to imported users
import-csv $path | Get-MSOLUser | out-gridview

Suggested reading

CodeTwo Office 365 Migration: free software for Office 365 subscribers

How to connect to Microsoft 365 with Microsoft Graph PowerShell

Tools for Exchange Server

Recommended articles

How to migrate from Exchange Server 2016/2019 to Microsoft 365

How to migrate from Exchange Server 2016/2019 to Microsoft 365

Migrating Exchange data to the cloud is not rocket science – explore your options and launch the migration stress-free. As you may already know, Exchange Server 2016 and 2019 have reached end of life and are no longer officially supported by Microsoft. If your organization still uses either of these platforms to manage email, contacts, calendars, and tasks, keep in mind that: Your environment may become vulnerable to newly discovered security threats – Microsoft will no longer provide security updates for Exchange 2016 and 2019 (unless you’ve enrolled in the Extended Security Update program, which ends in October 2026). Your emails may get blocked – Microsoft has started to throttle and block emails sent from unsupported Exchange Server versions to Exchange Online (as I covered in this article). That said, migrating to a supported platform is now the only viable long-term option for keeping your organization’s email environment secure, supported, and fully operational. If your organization wants or needs to keep things on‑premises (and continue using Microsoft’s solutions for that), upgrading to Exchange Server Subscription Edition (SE) is the only path forward. But given Microsoft’s clear preference for its cloud services – evident in the faster rollout of new features and the many security capabilities available exclusively in Microsoft 365 (Office 365) – now is a great time to leave your on‑prem environment behind and migrate to Exchange Online as part of Microsoft 365. While switching over to a new platform might seem like a rough ride, I’ll show you some easy ways to follow when migrating mailboxes from Exchange Server 2016/2019 to Microsoft 365. How to prepare for email migration to Microsoft 365 Before you start the migration process, you need to make sure your environment is ready for the move. For this purpose, you can use this guide in the Microsoft 365 admin center – it will help you connect your organization to Microsoft 365 and integrate your existing user accounts with Microsoft Entra ID. Microsoft also recommends completing the steps below: Set up an SPF record to determine valid email sources for your organization’s Microsoft 365 domain. Set up the Exchange Online Protection service as a means of protection against spam and malware. If you’re behind on updates, make sure to install the latest Cumulative Update (CU). And here is my quick, less obvious Microsoft 365 migration checklist: Verify if your software will work in Microsoft 365 (especially when it comes to server software). Microsoft 365 migration might be the time you learn that there is crucial legacy software that half the company uses and which is hard to replace. Encourage the whole company to clean up projects. It’s much easier to do this before the migration and start fresh. Gather as much information about your on‑premises environment as possible. For example, you might need to recreate access roles and permissions from scratch in the cloud or set up mail flow rules. Without prior research, it will be much more difficult. Verify if you need to migrate service accounts. There can be a lot of them on‑premises and in most cases, you won’t need them after the move. Review mailbox size limits in Exchange Online before migration to see which licenses you’ll need and whic
How to start remote PowerShell session to Exchange or Microsoft 365

How to start remote PowerShell session to Exchange or Microsoft 365

One of many features of the PowerShell command line tool is its ability to connect with and manage the Exchange Server remotely. The procedure described below applies to the classic on-prem Exchange server and to the Microsoft 365/Exchange Online version.
How to migrate Exchange public folders to a shared mailbox in Microsoft 365

How to migrate Exchange public folders to a shared mailbox in Microsoft 365

Still using public folders? Learn how to move them easily to a modern alternative with CodeTwo.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

CodeTwo sp. z o.o. sp. k. is a controller of your personal data.
See our Privacy Policy to learn more.