Forum Discussion

RachJ2255's avatar
RachJ2255
Copper Contributor
May 06, 2023

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...
  • AndySvints's avatar
    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.

Resources