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
Create, Remove And Assign License To An Office 365 User

Assigning License To User

To assign a license using a PowerShell script you have to run below codes
  1. #Check Sevice is present or not
  2. $service = Find - Module - NameMSOnline
  3. try {
  4. if ($service - eq$null) {
  5. Find - Module - NameMSOnline | Install - Module - Force
  6. }
  7. $crdentials = Get - Credential
  8. #Connect to MsolService
  9. Connect - MsolService - Credential$crdentials
  10. #get license details
  11. $license = Get - MsolAccountSku | Out - GridView - Title 'Please select a license plan you want to assign' - OutputModeSingle | Select - Object - ExpandPropertyAccountSkuId
  12. #Get user details
  13. $user = Get - MsolUser - All | Out - GridView - Title 'Please select a user you want to assign the license' - OutputModeMultiple
  14. #assigning license
  15. $user | Set - MsolUserLicense - AddLicenses$license
  16. Write - Host "Assigned license successfully to"
  17. $user.DisplayName
  18. } catch {
  19. Write - Host - ForegroundColorRed 'Error ', ':'
  20. $Error[0].ToString();
  21. sleep10
  22. }
Remove office365 User
To remove a office365 user using powershell, You have to run the following code.
  1. #Remove a user
  2. $spUser=Get-MsolUser-All|Out-GridView-Title'remove'-OutputModeMultiple
  3. $spUser|Remove-MSOLUser-Force
Run PowerShell Script
Right click on the PowerShell script. Then select Run with PowerShell.
Create, Remove And Assign License To An Office 365 User
A pop-up will open asking for credentials. Enter the SharePoint username and password.
Create, Remove And Assign License To An Office 365 User
Then another pop will open, Select your license (accountSkuId) that you want to assign. Select a license and click on "Ok"
ActiveUnits (The number of active license)
WarningUnits (The number of warning units)
ConsumedUnits (No of licensed consumed)
Create, Remove And Assign License To An Office 365 User
After another dialogue box opens, select a user and then click on "Ok".
Display Name (Show the name of a user)
Is Licensed (Show "True" and "False" for whether the user have licensed or not)
Create, Remove And Assign License To An Office 365 User
Another dialogue box will open, asking for removing a user. Then select a user and click on "Ok".
Note:If you did not want to remove office 365 user, then keep it blank.
Create, Remove And Assign License To An Office 365 User

Conclusion

The script is helpful and quick while assigning a license to a greater number of users. It will save time as well. We can select the required license, which we need to assign and then run to get the result.