Forum Discussion
RachJ2255
May 06, 2023Copper Contributor
get-Mguser: How to get more than 100 users when looping through userIds
I need to query Azure Ad to find certain groups and output the group name and the user details for each one. I have a script which works perfectly well, EXCEPT where the group has more than 100 user...
- May 06, 2023
Hello RachJ2255,
If I understood your use case correctly -All should do the trick for you.
You just need to add it to Get-MgGroupMember not Get-MgUser.l cmdlet.
Like this:
$groups = Get-MgGroup -ConsistencyLevel eventual -Count groupCount -Filter "startswith(DisplayName, 'XX-XXX')"|Select DisplayName, Id foreach ($group in $groups) { $members = Get-MgGroupMember -GroupId $group.Id -All foreach ($member in $members) { $user = Get-MgUser -UserId $member.Id $ObjectId = $user.Id; $UserDisplayname = $user.DisplayName; $userPrincipalName =$user.Mail; $GroupDisplayname = $group.DisplayName; #and then here I create an insert SQL statement to persist the results in a table } }
Hope that helps.
AndySvints
May 06, 2023Steel Contributor
Hello RachJ2255,
If I understood your use case correctly -All should do the trick for you.
You just need to add it to Get-MgGroupMember not Get-MgUser.l cmdlet.
Like this:
$groups = Get-MgGroup -ConsistencyLevel eventual -Count groupCount -Filter "startswith(DisplayName, 'XX-XXX')"|Select DisplayName, Id
foreach ($group in $groups) {
$members = Get-MgGroupMember -GroupId $group.Id -All
foreach ($member in $members) {
$user = Get-MgUser -UserId $member.Id
$ObjectId = $user.Id;
$UserDisplayname = $user.DisplayName;
$userPrincipalName =$user.Mail;
$GroupDisplayname = $group.DisplayName;
#and then here I create an insert SQL statement to persist the results in a table
}
}
Hope that helps.
RachJ2255
May 07, 2023Copper Contributor
AndySvints Thank you so much! That one simple addition does the trick, can't believe I didn't try it but anyway, you have saved my life!