Monday, 8 February 2016

Get all Licensed Office 365 users with PowerShell

It is very common requirement to get all the licensed users within an Office 365 tenant. We can use the Azure AD powershell cmdlet Get-MsolUser to list all the licensed office 365 users.

Note: Before proceed, Install and Configure Azure AD PowerShell

The following command just list the licensed office 365 users
Get-MsolUser -All | Where-Object { $_.isLicensed -eq ”TRUE” } | Select-Object UserPrincipalName, DisplayName, Department
The following command exports all the licensed users to csv file
Get-MsolUser -All | Where-Object { $_.isLicensed -eq ”TRUE” } | Select-Object UserPrincipalName, DisplayName, Department | Export-Csv C:\LicensedUsers.csv  -NoTypeInformation -Encoding UTF8
The above command just tell whether an user is licensed or not, doesn't list what kind of license has been applied to the users. The following powershell script get the detailed license plans and subscriptions that applied to every office 365 users and exports the output to csv file by using Export-CSV cmdlet.
$users = Get-MsolUser -All | Where-Object { $_.isLicensed -eq ”TRUE” }
$users | Foreach-Object{ 
  $licenseDetail = '' 
  $licenses='' 
  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 @{    
    UserName=$_.DisplayName  
    IsLicensed=$_.IsLicensed 
    Licenses=$licenses 
    LicenseDetails=$licenseDetail }
}  | Select UserName,IsLicensed,Licenses,LicenseDetails |
Export-CSV "C:\\Office-365-User-License-Report.csv" -NoTypeInformation -Encoding UTF8

Advertisements
Advertisements

No comments:

Post a Comment