skip to Main Content

I’m trying to create a report with all Microsoft licenses assigned to all users.

I know from PowerShell List all O365 Users and Associated Licenses

that I can use MSOnline for this or AzureAD commands too, but I would like to try with Graph.

I was trying to use in a loop

get-mguserlicensedetail -userid xxxx

I would like it in the format

User UPN SkuPartNumber
User 1 License 1
User 1 License 2
User 2 License 1
User 3 License 1
User 3 License 2

Regular get-mguserlicensedetail -userid [email protected]

returns

Id                                          SkuId                                SkuPartNumber
--                                          -----                                -------------
TBC6E6Ib4E2h7NkDTvMBp5K4DfPpB-lHg3yAcn9G_T0 f30db892-07e9-47e9-837c-80727f46fd3d FLOW_FREE
TBC6E6Ib4E2h7NkDTvMBp-7E6wa1G91HgSARMkvFTgY 06ebc4ee-1bb5-47dd-8120-11324bc54e06 SPE_E5

I tried to use the following (I had it prepared for all users but for testing used a smaller number), based on PowerShell List all O365 Users and Associated Licenses :

Connect-MgGraph -Scopes "User.Read.All"

# $all_users = Get-MgUser -All
# $users = $all_users.UserPrincipalName
$users = "[email protected]", "[email protected]", "[email protected]", "[email protected]"

foreach ($user in $users)
{
    $licenses_data = get-mguserlicensedetail -userid $user

    foreach ($x in $licenses_data)
    {
        $licenses = $x.SkuPartNumber
        foreach ($license in $licenses)
        {
            $result = [PSCustomObject]
            @{
                UPN = $user
                License = $licenses
            }
        }
    }
}

$result
Disconnect-MgGraph

It returned this:

Name                           Value
----                           -----
UPN                            [email protected]
License                        SPE_E5
UPN                            [email protected]
License                        FLOW_FREE
UPN                            [email protected]
License                        PBI_PREMIUM_PER_USER_ADDON
UPN                            [email protected]
License                        FLOW_FREE
UPN                            [email protected]
License                        SPE_E5
UPN                            [email protected]
License                        POWERAPPS_DEV
UPN                            [email protected]
License                        PBI_PREMIUM_PER_USER_ADDON
UPN                            [email protected]
License                        SPE_E5
UPN                            [email protected]
License                        POWERAPPS_VIRAL
UPN                            [email protected]
License                        FLOW_FREE
UPN                            [email protected]
License                        SPE_E5

IsCollectible              : False
DeclaringMethod            : 
FullName                   : System.Management.Automation.PSObject
AssemblyQualifiedName      : System.Management.Automation.PSObject, System.Management.Automation, 

after is collectible there is a lot more but I do not want to spam this question much more.
I know this aproach is probably really bad but it is my irst time that I try to create a report like this.

2

Answers


  1. Chosen as BEST ANSWER

    Thank @jdweng for your help.

    I have managed to resolve the issue myself - turns out Export-csv simply does what I need (I forgot to underline that I would export the data eventually, my bad here).

    Code that is working for me:

    Connect-MgGraph -Scopes "User.Read.All"
    
    $users = "[email protected]", "[email protected]", "[email protected]", "[email protected]"
    
    foreach ($user in $users)
    {
        $data = Get-MgUserLicenseDetail -UserId $user
        $data | Add-Member -Name User -Value $user -MemberType NoteProperty
        $data | Export-Csv -Path .data.csv -Append
    }
    
    Disconnect-MgGraph
    

  2. Try following :

    onnect-MgGraph -Scopes "User.Read.All"
    
    # $all_users = Get-MgUser -All
    # $users = $all_users.UserPrincipalName
    $users = "[email protected]", "[email protected]", "[email protected]", "[email protected]"
    $table = [System.Collections.ArrayList]::new()
    foreach ($user in $users)
    {
        $licenses_data = get-mguserlicensedetail -userid $user
    
        foreach ($x in $licenses_data)
        {
            $licenses = $x.SkuPartNumber
            foreach ($license in $licenses)
            {
                $result = [PSCustomObject]
                @{
                    UPN = $user
                    License = $licenses
                }
                $table.Add($result) | Out-Null
            }
        }
    }
    $table
    Disconnect-MgGraph
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search