# Checking if a license is assigned directly or via a group

**URL:** https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306
**Category:** Open Discussions
**Created:** [December 18, 2023, 4:24pm UTC](https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306 "2023-12-18T16:24:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mitchem](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mitchem](https://forums.powershell.org/u/mitchem)
#### Post date: [December 18, 2023, 4:24pm UTC](https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306/1 "2023-12-18T16:24:40Z")

</div>

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](https://learn.microsoft.com/en-us/entra/identity/users/licensing-ps-examples)) 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

---

<div class="post-metadata">

### Author: ![Austin\_H](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/austin_h/32/4523_2.png) [@Austin\_H](https://forums.powershell.org/u/Austin_H)
#### Post date: [December 18, 2023, 4:32pm UTC](https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306/2 "2023-12-18T16:32:18Z")

</div>

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif)

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.

```auto
#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) } }

```

---

<div class="post-metadata">

### Author: ![mitchem](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@mitchem](https://forums.powershell.org/u/mitchem)
#### Post date: [December 18, 2023, 5:18pm UTC](https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306/3 "2023-12-18T17:18:03Z")

</div>

Hi Dude9crusher.

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

Hopefully this clarifies.

Thanks

````auto
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)}}```
````

---

<div class="post-metadata">

### Author: ![psjamesp](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/psjamesp/32/959_2.png) [@psjamesp](https://forums.powershell.org/u/psjamesp)
#### Post date: [December 18, 2023, 7:15pm UTC](https://forums.powershell.org/t/checking-if-a-license-is-assigned-directly-or-via-a-group/23306/4 "2023-12-18T19:15:45Z")

</div>

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](https://learn.microsoft.com/en-us/microsoft-365/enterprise/view-licensed-and-unlicensed-users-with-microsoft-365-powershell?view=o365-worldwide)
