Pull a list of all Microsoft Licenses and Service Plans for all users

Scenario: You need to pull all licenses assigned to all users in your Azure Online tenant.

Solution: LETS GRAPH IT in PowerShell

#0. Install MSGraph PS Plugin if you dont have it
    Install-Module Microsoft.Graph

#1. Connect to Graph
    Connect-MgGraph -Scopes 'User.Read.All'

#2. Pull all License SKUIDs
    $LicenseSkuID = Get-MgSubscribedSku -All
    $LicenseIndex = Import-csv C:\temp\Microsoft_License_Products.csv #Download the CSV from this site: https://learn.microsoft.com/en-us/azure/active-directory/enterprise-users/licensing-service-plan-reference

#3. Get list of Users
     $users = Get-MgUser -All

#4. Report it and Loop through users to find licenses
$report = @()
$users | %{

        #Create Variable
        $u = $_.UserPrincipalName

        "Pulling licenses for $U"

        #Get license for each user
            [array]$AllLicenses = Get-MgUserLicenseDetail -UserId $u

        #Loop through Licenses
            $AllLicenses | %{
                $SKUID = $_.SkuID
                $SkuPartNumber = $_.SkuPartNumber
                $ServicePlans = $_.ServicePlans
                $Friendly_ProductName = $LicenseIndex | Where GUID -eq $SKUID | Select -ExpandProperty Product_Display_Name -Unique

                If($ServicePlans -ne $null){
                $ServicePlans | %{
                    $AppliesTo = $_.AppliesTo
                    $ProvisioningStatus = $_.provisioningStatus
                    $ServicePlanID = $_.servicePlanID
                    $ServicePlanName = $_.ServicePlanName
                    $Friendly_Name = $LicenseIndex | Where {($_.GUID -eq $SKUID) -and ($_.Service_Plan_Name -eq $ServicePlanName)} | select -ExpandProperty Service_Plans_Included_Friendly_Names

                     $obj = new-object psObject
                        $obj | Add-Member -membertype noteproperty -Name UPN -Value $U
                        $obj | Add-Member -membertype noteproperty -Name SKUID -Value $SKUID
                        $obj | Add-Member -membertype noteproperty -Name SKUPartNumber -Value $SKUPartNumber
                        $obj | Add-Member -membertype noteproperty -Name SKUFriendlyProductName -Value $Friendly_ProductName
                        $obj | Add-Member -membertype noteproperty -Name ServicePlanID -Value $ServicePlanID
                        $obj | Add-Member -membertype noteproperty -Name ServicePlanName -Value $Serviceplanname
                        $obj | Add-Member -membertype noteproperty -Name ServicePlanStatus -Value $ProvisioningStatus
                        $obj | Add-Member -membertype noteproperty -Name ServicePlanAppliesTo -Value $AppliesTo
                        $obj | Add-Member -membertype noteproperty -Name ServicePlanFriendlyName -Value $Friendly_name
                        $report += $obj



#5. Display the Report


    $Report | Export-csv C:\temp\report.csv

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: