Hi Comp_tech, I want to help you with this question.
To export each license for a specific user in a specific group you have to loop through each Object to flatten the output.
I would do it this way:
$groups = @(Group1, Group2, ....)
$users = @()
foreach ($group in $groups) {
$groupObj = Get-MgGroup -Filter "Displayname $($group)"
foreach ($member in $members) {
$user = Get-MgUser -UserId $member.Id -Property ID,DisplayName,UserPrincipalName,companyName,onPremisesExtensionAttributes | Select-Object ID,DisplayName,UserPrincipalName,companyName -ExpandProperty onPremisesExtensionAttributes ExtensionAttribute9, ExtensionAttribute10, ExtensionAttribute11, ExtensionAttribute12
$licenses = $null
$licenses = (Get-MgUserLicenseDetail -UserId $User.id).SkuPartNumber
foreach ($license in $licenses) {
Write-Host "Licenses found for $($User.DisplayName): $license"
$users += New-Object PSObject -Property @{
ID = $user.ID
USERPRINCIPALNAME = $user.Mail
Name = $user.DisplayName
Group = $groupObj.DisplayName
Licenses = $license
Company = $user.CompanyName
ExtensionAttribute9 = $user.ExtensionsAttribute9
[AND SO ON...]
}
}
}
}
Please keep in mind that this solution will eventually return multiple objects for each user if they are in multiple groups.
If the reply was helpful, please don’t forget to upvote or accept it as an answer, thank you.