SOLVED

get-Mguser: How to get more than 100 users when looping through userIds

Copper Contributor

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 users, when it only brings back 100. How do i get around this - I can see from the documentation that the -All parameter doe snot work with the -UserId parameter, but I need to only see the users in the groups I select.

 

Any ideas on how to get around this issue would be very gratefully received.

The basic script is as follows:

 

$groups=@()
$groups = get-MgGroup -ConsistencyLevel eventual -Count groupCount -Filter "startswith(DisplayName, 'XX-XXX')"|Select DisplayName, Id

$members= @()
foreach ($group in $groups) {
$members = Get-MgGroupMember -GroupId $group.Id
#$users = @()

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

}

2 Replies
best response confirmed by RachJ2255 (Copper Contributor)
Solution

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 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!