Forum Discussion
fstorer
Jan 17, 2025Brass Contributor
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!
- Here you go
#Connect to Microsoft GraphConnect-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 MembersForeach ($Member in $GroupMembers){#Get Entra User$EntraUser = Get-MgUser -UserId $Member.ID -Property DisplayName, Id, Mail, UserPrincipalName, AgeGroup$UPN = $EntraUser.UserPrincipalName$AgeGroup = $EntraUser.AgeGroupWrite-Host "$UPN > $AgeGroup"If ($AgeGroup -eq "Minor"){#Add to Array$MinorGroupMembers += $UPN}}$MinorGroupMembers
- Andres-BohrenSteel ContributorHere you go
#Connect to Microsoft GraphConnect-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 MembersForeach ($Member in $GroupMembers){#Get Entra User$EntraUser = Get-MgUser -UserId $Member.ID -Property DisplayName, Id, Mail, UserPrincipalName, AgeGroup$UPN = $EntraUser.UserPrincipalName$AgeGroup = $EntraUser.AgeGroupWrite-Host "$UPN > $AgeGroup"If ($AgeGroup -eq "Minor"){#Add to Array$MinorGroupMembers += $UPN}}$MinorGroupMembers - lucheteSteel 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!