Export name of O365 groups a user is member of

Hello,
I am trying to get the list of all O365 groups a user is member of. I tried the above but ran into issues.

$UserEmail= "emailaddress"
$Mailbox = Get-Mailbox | Where {$_.PrimarySmtpAddress -eq $UserEmail}
$Office365GroupsMember = Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Members | foreach {$_.name}) -contains $mailbox.Alias}

type or paste code here

I got the error message

*WARNING: By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items re
turned. To return all items, specify "-ResultSize Unlimited". Be aware that, depending on the actual number of items, retur
ning 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.

How can I fix this?*

Titan81,
Welcome to the forum. :wave:t4:

Have you actually read the error message? It’s one of the few PowerShell error message where the solution comes with the error message. :wink:

Regardless of that … it does not make any sense to query ALL mailboxes …

$Mailbox = Get-Mailbox | Where {$_.PrimarySmtpAddress -eq $UserEmail}

… when you actually know which one exactly you want. You should query only this particular mailbox:

$Mailbox = Get-Mailbox -Identity $UserEmail

So assumed you don’t have more than 1000 Unified Groups this should be enough:

$UserEmail= 'emailaddress'
$Mailbox = Get-Mailbox -Identity $UserEmail
Get-UnifiedGroup | Where-Object { (Get-UnifiedGroupLinks -Identity $_.Alias -LinkType Members).name -contains $mailbox.Alias}

Thanks.
I tried this and it is still displaying the same error message. I am new at powershell and trying to learn.

By default, only the first 1000 items are returned. Use the ResultSize parameter to specify the number of items re
turned. To return all items, specify “-ResultSize Unlimited”. Be aware that, depending on the actual number of items, retur
ning 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.

You tried what? You didn’t show your code. The filter must not have worked if it’s still stating it returned more than 1000. Please show your latest attempt