Saturday, 14 November 2015

Manage Office 365 Users License using Powershell

We can use the Azure AD Powershell cmdlet Set-MsolUserLicense to manage Office 365 license for a user. You can add a new license, remove an existing license and update existing license options (enable or disable license plans) using this cmdlet.


Add New License to Office 365 User:

To add a new license, we need to get AccountSkuId for the particular license, you can list all the available licenses and its AccountSkuId by running the following command.

Note: Before proceed, Install and Configure Azure AD PowerShell
Get-MsolAccountSku | Select AccountSkuId,ActiveUnits,ConsumedUnits
You can also get the list of all the available service plans in a particular license by using below command:
Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq 'mts:O365_BUSINESS_PREMIUM'} | ForEach-Object {$_.ServiceStatus}
Next, we need to set Usage Location for the user. This is required for every individual user. If you are re-assigning the license, the usage location is not mandatory. We have to use the Azure AD powershell cmdlet Set-MsolUser to set usage location.
Set-MsolUser -UserPrincipalName '' -UsageLocation "US"
Now, we can assign the new license by using below powershell command.
Set-MsolUserLicense -UserPrincipalName '' -AddLicenses 'mts:O365_BUSINESS_PREMIUM'
To add multiple licenses, you have to give all license's AccountSkuId as comma (,) separated values.
Set-MsolUserLicense -UserPrincipalName '' -AddLicenses mts:O365_BUSINESS_PREMIUM,mts:AAD_PREMIUM

Enable/Disable License Options (License feature):

While adding new licene to an user, we can enable or disable only particular set of sub components of new license. we have to use the powershell cmdlet New-MsolLicenseOptions to set license plans or features that we want to disable (or remove) from new license.
$options = New-MsolLicenseOptions -AccountSkuId 'mts:O365_BUSINESS_PREMIUM' -DisabledPlans OFFICE_BUSINESS,MCOSTANDARD
Now, we can pass this license options to Set-MsolUserLicense cmdlet using the paramater -LicenseOptions.
Set-MsolUserLicense -UserPrincipalName '' –AddLicenses 'mts:O365_BUSINESS_PREMIUM' -LicenseOptions $options
Note: If you want to update or disable license plans in existing license, you have to set only LicenseOptions in Set-MsolUserLicense cmdlet.
Set-MsolUserLicense -UserPrincipalName '' -LicenseOptions $options

Re-Assigning License of Office 365 User:

We can easily re-assign or replace new license by removing old license by using single command. The below powershell command remove the license mts:O365_BUSINESS_PREMIUM from the user morgan and add the license mts:ENTERPRISEPACK.
Set-MsolUserLicense -UserPrincipalName '' -AddLicenses 'mts:ENTERPRISEPACK' -RemoveLicenses 'mts:O365_BUSINESS_PREMIUM'

Remove License from Office 365 User:

We can easily remove an existing license from an user by passing the license's AccountSkuId to the parameter –RemoveLicenses in Set-MsolUserLicense cmdlet.
Set-MsolUserLicense -UserPrincipalName '' –RemoveLicenses 'mts:O365_BUSINESS_PREMIUM'

Find and Export User License Details:

We can find and get the applied license details of all the office 365 users by using the Azure AD powershell cmdlet Get-MsolUser. The Get-MsolUser cmdlet returns the user's license details and applied license's sub components (enabled license plans).
$users = Get-MsolUser | Where-Object { $_.IsLicensed }
$users | Foreach-Object{ 
  $licenseDetail = '' 
  if($_.licenses -ne $null) {
ForEach ($license in $_.licenses){
  switch -wildcard ($($license.Accountskuid.tostring())) { 
           '*POWER_BI_STANDALONE' { $licName = 'POWER BI STANDALONE' } 
           '*CRMSTANDARD' { $licName = 'CRM Online' }
           '*O365_BUSINESS_PREMIUM' { $licName = 'Office 365 BUSINESS PREMIUM' } 
           '*ENTERPRISEPACK' { $licName = 'Office 365 (Plan E3)' }  
           default { $licName = $license.Accountskuid.tostring() }

  if($licenses){  $licenses = ($licenses + ',' + $licName) } else { $licenses = $licName}
ForEach ($row in $($license.servicestatus)) {

if($row.ProvisioningStatus -ne 'Disabled') {          
       switch -wildcard ($($row.ServicePlan.servicename)) { 
           'EXC*' { $thisLicence = 'Exchange Online' }  
           'LYN*' { $thisLicence = 'Skype for Business' }
           'SHA*' { $thisLicence = 'Sharepoint Online' }       
           default { $thisLicence = $row.ServicePlan.servicename }  
 if($licenseDetail){ $licenseDetail = ($licenseDetail + ',' + $thisLicence) }  Else { $licenseDetail = $thisLicence}}
New-Object -TypeName PSObject -Property @{    
    LicenseDetails=$licenseDetail }
}  | Select UserName,Licenses,LicenseDetails |
Export-CSV "C:\\Office-365-User-License-Report.csv" -NoTypeInformation -Encoding UTF8


No comments:

Post a Comment