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.
A pop-up will open asking for credentials. Enter the SharePoint username and password.
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)
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)
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.
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.