Attempting to retrieve a list of all (Unified Groups) of which given users are members

Copper Contributor

I have a script in design to give me unified group memberships for a supplied list of users.

I have tried the following:

 

$userFQDN = Get-User $user.DisplayName | select -ExpandProperty DistinguishedName
$userMemberships = Get-Recipient -Filter "Members -eq '$userFQDN'" -RecipientTypeDetails GroupMailbox,MailUniversalDistributionGroup,MailUniversalSecurityGroup

 

The above snippet returns nothing quickly suggesting an issue with the '-filter' parameter.

I have also tried this:

 

$userMailbox =  Get-EXOMailbox | Where {$_.PrimarySmtpAddress -eq $User.UserPrincipalName} | Select PrimarySmtpAddress, Alias
$userUnifiedGroups = Get-UnifiedGroup -ResultSize Unlimited | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Members | foreach {$_.name}) -contains $userMailbox.Alias}

 

I am getting the below response which is suggesting the selection criteria are not as efficient as I'd anticipated.  (We do have a lot of O365 Groups.)

 

WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual numb
er of items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task or script to perform batch changes.

WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items returned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual numb
er of items, returning all items can take a long time and consume a large amount of memory. Also, we don't recommend storing the results in a variable. Instead, pipe the results to another task or script to perform batch changes.

 

Both snippets are in the context of a 'foreach ($user in <object collection>) {}'.

Thanks in advance.

Q

4 Replies

Using the filter is the fastest/easiest way to do this, and it should work just fine. Make sure you are passing a proper value for the DN though.

Hi Vasil

Thanks for your reply.

Are you able to improve my -filter format, please?

Thanks

QW

The format is fine, just make sure you are passing the correct DN.

Hi @Vasil Michev 

I'm getting confused around the format - which type of quotes to use?

In particular when using a variable to specify the comparison string.

Thanks

QW