Assign a License to an Office 365 User with PowerShell
There are several scenarios where you might need to assign an Office 365 license to a user. The specific scenario in this blog article is that you're migrating an Exchange Server 2010 on-premises environment to Office 365. The Exchange Server is already in hybrid mode. Users have been automatically created in Office 365 by synchronizing them from your on-premises Active Directory environment using Azure AD Connect. Users who haven't already had their mailbox moved to Office 365 will first need an Office 365 license assigned to them, and before a license can be assigned to them, a usage location must be set on their individual account.
This blog article is written using Windows 10 Enterprise Edition version 1803 and Windows PowerShell version 5.1. The examples shown in this blog article will not work with PowerShell Core. Your mileage may vary with other operating systems and other versions of PowerShell.
First, you'll need the cmdlets to perform these actions. Find the MSOnline module in the PowerShell Gallery.
1Find-Module -Name MSOnline
Install the MSOnline module from the PowerShell Gallery:
1Find-Module -Name MSOnline | Install-Module -Force
Store your Office 365 credentials with sufficient access to perform these tasks in a variable.
1$O365Cred = Get-Credential
Connect to your Office 365 account. This is the part that will generate an error if you're using PowerShell Core.
1Connect-MsolService -Credential $O365Cred
Check to see if you have more than one Office 365 subscription.
1Get-MsolAccountSku
Store the specific account SKU with the licenses for the Office 365 subscription to assign to users in a variable.
1$LicenseSKU = Get-MsolAccountSku |
2 Out-GridView -Title 'Select a license plan to assign to users' -OutputMode Single |
3 Select-Object -ExpandProperty AccountSkuId
Find the users to assign licenses to and store them in a variable. I found it useful to narrow these
results down by filtering left with the UserPrincipalName
and/or Department
parameters of
Get-MsolUser.
1$Users = Get-MsolUser -All -UnlicensedUsersOnly |
2Out-GridView -Title 'Select users to assign license plan to' -OutputMode Multiple
As you can see in the previous image, John Doe does not currently have a license assigned.
Assign a usage location.
1$Users | Set-MsolUser -UsageLocation US
Assign an Office 365 license.
1$Users | Set-MsolUserLicense -AddLicenses $LicenseSKU
A license has now been assigned to John Doe.
Although a single user was assigned a license, with the exception of the previous command, the code as it is written in this blog article can be used to assigned licenses to multiple users.
µ