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 user1@example.com
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 = "user1@example.com", "user2@example.com", "user3@example.com", "user4@example.com"
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 user1@example.com
License SPE_E5
UPN user1@example.com
License FLOW_FREE
UPN user2@example.com
License PBI_PREMIUM_PER_USER_ADDON
UPN user2@example.com
License FLOW_FREE
UPN user2@example.com
License SPE_E5
UPN user2@example.com
License POWERAPPS_DEV
UPN user3@example.com
License PBI_PREMIUM_PER_USER_ADDON
UPN user3@example.com
License SPE_E5
UPN user3@example.com
License POWERAPPS_VIRAL
UPN user4@example.com
License FLOW_FREE
UPN user4@example.com
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.
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 = "user1@example.com", "user2@example.com", "user3@example.com", "user4@example.com"
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