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

%3CLINGO-SUB%20id%3D%22lingo-sub-1972640%22%20slang%3D%22en-US%22%3EAttempting%20to%20retrieve%20a%20list%20of%20all%20(Unified%20Groups)%20of%20which%20given%20users%20are%20members%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1972640%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22thread-message-content-body-text%20thread-full-message%22%3E%3CP%3EI%20have%20a%20script%20in%20design%20to%20give%20me%20unified%20group%20memberships%20for%20a%20supplied%20list%20of%20users.%3C%2FP%3E%3CP%3EI%20have%20tried%20the%20following%3A%3C%2FP%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24userFQDN%20%3D%20Get-User%20%24user.DisplayName%20%7C%20select%20-ExpandProperty%20DistinguishedName%0A%24userMemberships%20%3D%20Get-Recipient%20-Filter%20%22Members%20-eq%20'%24userFQDN'%22%20-RecipientTypeDetails%20GroupMailbox%2CMailUniversalDistributionGroup%2CMailUniversalSecurityGroup%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20above%20snippet%20returns%20nothing%20quickly%20suggesting%20an%20issue%20with%20the%20'-filter'%20parameter.%3C%2FP%3E%3CP%3EI%20have%20also%20tried%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24userMailbox%20%3D%26nbsp%3B%20Get-EXOMailbox%20%7C%20Where%20%7B%24_.PrimarySmtpAddress%20-eq%20%24User.UserPrincipalName%7D%20%7C%20Select%20PrimarySmtpAddress%2C%20Alias%0A%24userUnifiedGroups%20%3D%20Get-UnifiedGroup%20-ResultSize%20Unlimited%20%7C%20where%20%7B%20(Get-UnifiedGroupLinks%20%24_.Alias%20-LinkType%20Members%20%7C%20foreach%20%7B%24_.name%7D)%20-contains%20%24userMailbox.Alias%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22contentStandard%22%3E%3CSPAN%3EI%20am%20getting%20the%20below%20response%20which%20is%20suggesting%20the%20selection%20criteria%20are%20not%20as%20efficient%20as%20I'd%20anticipated.%26nbsp%3B%20(We%20do%20have%20a%20lot%20of%20O365%20Groups.)%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EWARNING%3A%20By%20default%2C%20only%20the%20first%201000%20items%20are%20returned.%20Use%20the%20ResultSize%20parameter%20to%20specify%20the%20number%20of%20items%20returned.%20To%20return%20all%20items%2C%20specify%20%22-ResultSize%20Unlimited%22.%20Be%20aware%20that%2C%20depending%20on%20the%20actual%20numb%0Aer%20of%20items%2C%20returning%20all%20items%20can%20take%20a%20long%20time%20and%20consume%20a%20large%20amount%20of%20memory.%20Also%2C%20we%20don't%20recommend%20storing%20the%20results%20in%20a%20variable.%20Instead%2C%20pipe%20the%20results%20to%20another%20task%20or%20script%20to%20perform%20batch%20changes.%0A%0AWARNING%3A%20By%20default%2C%20only%20the%20first%201000%20items%20are%20returned.%20Use%20the%20ResultSize%20parameter%20to%20specify%20the%20number%20of%20items%20returned.%20To%20return%20all%20items%2C%20specify%20%22-ResultSize%20Unlimited%22.%20Be%20aware%20that%2C%20depending%20on%20the%20actual%20numb%0Aer%20of%20items%2C%20returning%20all%20items%20can%20take%20a%20long%20time%20and%20consume%20a%20large%20amount%20of%20memory.%20Also%2C%20we%20don't%20recommend%20storing%20the%20results%20in%20a%20variable.%20Instead%2C%20pipe%20the%20results%20to%20another%20task%20or%20script%20to%20perform%20batch%20changes.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22contentStandard%22%3E%3CSPAN%3EBoth%20snippets%20are%20in%20the%20context%20of%20a%20'foreach%20(%24user%20in%20%3COBJECT%20collection%3D%22%22%3E)%20%7B%7D'.%3C%2FOBJECT%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22contentStandard%22%3E%3CSPAN%3EThanks%20in%20advance.%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22contentStandard%22%3E%3CSPAN%3EQ%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1972640%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExchange%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1972702%22%20slang%3D%22en-US%22%3ERe%3A%20Attempting%20to%20retrieve%20a%20list%20of%20all%20(Unified%20Groups)%20of%20which%20given%20users%20are%20members%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1972702%22%20slang%3D%22en-US%22%3E%3CP%3EUsing%20the%20filter%20is%20the%20fastest%2Feasiest%20way%20to%20do%20this%2C%20and%20it%20should%20work%20just%20fine.%20Make%20sure%20you%20are%20passing%20a%20proper%20value%20for%20the%20DN%20though.%3C%2FP%3E%3C%2FLINGO-BODY%3E
New 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