Forum Discussion

fstorer's avatar
fstorer
Brass Contributor
Jan 17, 2025
Solved

Get a list of specific agegroup users stored on a security group

Dear Community,

I wonder if it would be possible to get a list of users (stored in a security group) marked as "minor" and "not adult" using microsoft graph. Once I get the members of the group (using Get-MgGroupMember -GroupId XXXX), I am not sure how to retrieve only the ones with a specific agegroup property. Is that feasible?

Any help would be greatly appreciated. Many thanks in advance!

 

  • fstorer 

    Here you go

    #Connect to Microsoft Graph
    Connect-MgGraph -Scopes 'Group.Read.All'
     
    #Create empty Array
    $MinorGroupMembers = @()
     
    #Get Entra Group
    $EntraGroup = Get-MgGroup -Filter "DisplayName eq 'AAD-AgeGroupMembers'"
     
    #Get Entra Group Members
    $GroupMembers = Get-MgGroupMember -GroupId $EntraGroup.ID
     
    #Loop through Members
    Foreach ($Member in $GroupMembers)
    {
    #Get Entra User
    $EntraUser = Get-MgUser -UserId $Member.ID -Property DisplayName, Id, Mail, UserPrincipalName, AgeGroup
    $UPN = $EntraUser.UserPrincipalName
    $AgeGroup = $EntraUser.AgeGroup
    Write-Host "$UPN > $AgeGroup"
     
    If ($AgeGroup -eq "Minor")
    {
    #Add to Array
    $MinorGroupMembers += $UPN
    }
    }
     
    $MinorGroupMembers
  • Andres-Bohren's avatar
    Andres-Bohren
    Steel Contributor

    fstorer 

    Here you go

    #Connect to Microsoft Graph
    Connect-MgGraph -Scopes 'Group.Read.All'
     
    #Create empty Array
    $MinorGroupMembers = @()
     
    #Get Entra Group
    $EntraGroup = Get-MgGroup -Filter "DisplayName eq 'AAD-AgeGroupMembers'"
     
    #Get Entra Group Members
    $GroupMembers = Get-MgGroupMember -GroupId $EntraGroup.ID
     
    #Loop through Members
    Foreach ($Member in $GroupMembers)
    {
    #Get Entra User
    $EntraUser = Get-MgUser -UserId $Member.ID -Property DisplayName, Id, Mail, UserPrincipalName, AgeGroup
    $UPN = $EntraUser.UserPrincipalName
    $AgeGroup = $EntraUser.AgeGroup
    Write-Host "$UPN > $AgeGroup"
     
    If ($AgeGroup -eq "Minor")
    {
    #Add to Array
    $MinorGroupMembers += $UPN
    }
    }
     
    $MinorGroupMembers
  • luchete's avatar
    luchete
    Steel Contributor

    Hello,

    You can use Microsoft Graph to retrieve the members of the security group, but filtering users by a specific age group property requires checking the "birthdate" or custom attributes. You can use "Get-MgUser" to get the user details and filter based on age using PowerShell's "Where-Object" cmdlet. For example, calculate the user's age from the "birthdate" and compare it to your desired age group. If your organization stores age-related data in custom attributes, you can also filter based on those attributes directly.

    Hope it helps!

Resources