This blog article is written about assigning a product license to an Office 365 user account using PowerShell. To do the operations using PowerShell you have to follow some instructions.
First, you need to make sure that Microsoft Online Service and AzureAD are installed on your management PC. If you have not installed it yet, follow the below commands to install those. Run your Windows PowerShell as Administrator then run commands.
- Install-Module-NameAzureAD
- Install-ModuleMSOnline
Create office365 user
To create an office 365 user, you can run the below command,
New-MsolUser-UserPrincipalNametsetuser2@sharepoint.onmicrosoft.com-DisplayName"TestUser2"-FirstNameTestUser-LastName2
You can also set a password and assign licenses at that time of creation of the user:-
New-MsolUser-UserPrincipalNametsetuser2@sharepoint.onmicrosoft.com-DisplayName"TestUser2"-FirstNameTestUser-LastName2-Password************-ForceChangePassword$true-LicenseAssignmentoffice365E3

Assigning License To User
To assign a license using a PowerShell script you have to run below codes
- #Check Sevice is present or not
- $service = Find - Module - NameMSOnline
- try {
- if ($service - eq$null) {
- Find - Module - NameMSOnline | Install - Module - Force
- }
- $crdentials = Get - Credential
- #Connect to MsolService
- Connect - MsolService - Credential$crdentials
- #get license details
- $license = Get - MsolAccountSku | Out - GridView - Title 'Please select a license plan you want to assign' - OutputModeSingle | Select - Object - ExpandPropertyAccountSkuId
- #Get user details
- $user = Get - MsolUser - All | Out - GridView - Title 'Please select a user you want to assign the license' - OutputModeMultiple
- #assigning license
- $user | Set - MsolUserLicense - AddLicenses$license
- Write - Host "Assigned license successfully to"
- $user.DisplayName
- } catch {
- Write - Host - ForegroundColorRed 'Error ', ':'
- $Error[0].ToString();
- sleep10
- }
Remove office365 User
To remove a office365 user using powershell, You have to run the following code.
- #Remove a user
- $spUser=Get-MsolUser-All|Out-GridView-Title'remove'-OutputModeMultiple
- $spUser|Remove-MSOLUser-Force
Run PowerShell Script
Right click on the PowerShell script. Then select Run with PowerShell.






Join the conversation! Your thoughts help the community grow.