Checking if a license is assigned directly or via a group

Hi all/ I need to understand whether a MS 365 is assigned directly or via a group, as we are moving to group assigned licenses and I need to run the check to ensure that all of our users are assigned via group not directly.

I have come across the following PowerShell (on PowerShell and Microsoft Graph examples for group licensing - Microsoft Entra ID | Microsoft Learn) which should work (but not for me)

#the license SKU we are interested in. use Get-MsolAccountSku to see a list of all identifiers in your organization
$skuId = “contoso:EMS”

#find all users that have the SKU license assigned
Get-MsolUser -All | where {$.isLicensed -eq $true -and $.Licenses.AccountSKUID -eq $skuId} | select ObjectId,
@{Name=“SkuId”;Expression={$skuId}}, @{Name="AssignedDirectly";Expression={(UserHasLicenseAssignedDirectly $_ $skuId)}},
@{Name=“AssignedFromGroup”;Expression={(UserHasLicenseAssignedFromGroup $_ $skuId)}}

I have changed the ‘Contoso’ to reflect our company name in the tenant. When I run the command I am expecting this:

ObjectId SkuId AssignedDirectly AssignedFromGroup


157870f6-e050-4b3c-ad5e-0f0a377c8f4d contoso:EMS True False
1f3174e2-ee9d-49e9-b917-e8d84650f895 contoso:EMS False True
240622ac-b9b8-4d50-94e2-dad19a3bf4b5 contoso:EMS True True

But what I get is this:

ObjectId SkuId AssignedDirectly AssignedFromGroup


157870f6-e050-4b3c-ad5e-0f0a377c8f4d contoso:EMS
1f3174e2-ee9d-49e9-b917-e8d84650f895 contoso:EMS
240622ac-b9b8-4d50-94e2-dad19a3bf4b5 contoso:EMS

I have activated teh following Azure roles, License manager, user manager, Billing Manager.

Anyone any ideas?

Thanks

Hi Mitchem,

Welcome to the forums first of all. Second, when you post code, please make sure that it adheres to the coding practices outlined here:
Codeformatting_1

I’ve assisted in the fixed code below as it looks like there appeared to be some missing code. Please confirm the code is correct to your needs. Otherwise, please provide a fixed sample.

#the license SKU we are interested in. use Get-MsolAccountSku to see a list of all identifiers in your organization
$skuId = “contoso:EMS”

#find all users that have the SKU license assigned
Get-MsolUser -All | Where-Object { ($_.isLicensed -eq $true) -and ($_.Licenses.AccountSKUID -eq $skuId) } | `
Select-Object ObjectId,@{Name = “SkuId”; Expression = { $skuId } }, @{Name = "AssignedDirectly"; Expression = { (UserHasLicenseAssignedDirectly $_ $skuId) } }, `
@{Name = “AssignedFromGroup”; Expression = { (UserHasLicenseAssignedFromGroup $_ $skuId) } }

Hi Dude9crusher.

Thanks for the pointers, and there is some missing code. The correct code is as follows:

Hopefully this clarifies.

Thanks

Connect-MsolService

#the license SKU we are interested in. use Get-MsolAccountSku to see a list of all identifiers in your organization
$skuId = "contoso:EMS"

#find all users that have the SKU license assigned
Get-MsolUser -All | where {$_.isLicensed -eq $true -and $_.Licenses.AccountSKUID -eq $skuId} | select `
    ObjectId, `
    @{Name="SkuId";Expression={$skuId}}, `
    @{Name="AssignedDirectly";Expression={(UserHasLicenseAssignedDirectly $_ $skuId)}}, `
    @{Name="AssignedFromGroup";Expression={(UserHasLicenseAssignedFromGroup $_ $skuId)}}```

Hey @mitchem again welcome to the forums.

If it were me I would take some time to look into switching your code to the Graph API. Here is a lean doc that may help. I will also try to work on some code later this evening. But specifically I suggest you look at Get-MgUserLicenseDetail

View licensed and unlicensed Microsoft 365 users with PowerShell - Microsoft 365 Enterprise | Microsoft Learn