theme-sticky-logo-alt

Find all the Office 365 Groups a user is a Member of with PowerShell

8 Comments

Whether you want to do some reports or debugging, you might need to quickly find out what Office 365 Groups a user is a member or Owner of! You could go in the user’s profile, see all the groups, find out which ones are Security, which ones are Distribution Groups, and which ones are Office 365 Groups. or you could do an easy PowerShell cmdlet!

Find all the Office 365 Groups a user is a Member of with PowerShell

Since we will be playing with Exchange Online, you first need to connect to Exchange!

$cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session

 

Next up, I will save the E-mail of the person I am looking for in a variable called UserEmail, and use the GetMailbox cmdlet to save the Mailbox object of that user in a variable called Mailbox.

 

$UserEmail= "Jeff.Collins@globomantics.org"
$Mailbox = Get-Mailbox | Where {$_.PrimarySmtpAddress -eq $UserEmail}

After we have this information, we can use the Get-UnifiedGroup PowerShell cmdlet, together with the Get-UnifiedGroupLinks cmdlet to find which groups this user is a member of!

To find all the Office 365 Groups a user is a member of, we would use the following cmdlet:

$Office365GroupsMember = Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Members | foreach {$_.name}) -contains $mailbox.Alias}

We then have all those groups stored in a variable, and we can use them as we wish inside PowerShell!

Find all the Office 365 Groups a user is a Member of with PowerShell

To find all the Office 365 Groups a user is an owner of, we would use the following cmdlet:

$Office365GroupsOwner = Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Owners| foreach {$_.name}) -contains $mailbox.Alias}

As you see, the only thing that changed is the -LinkType parameter from Members to Owners! We now also have all those groups stored in a variable to use as we wish in PowerShell!

Find all the Office 365 Groups a user is a Member of with PowerShell

 

Follow me on Social Media and Share this article with your friends!

Leave a comment and don’t forget to like the Vlad Talks Tech Page   on Facebook and to follow me on Twitter here  for the latest news and technical articles on SharePoint.  I am also a Pluralsight author, and you can view all the courses I created on my author page.
Previous Post
Find all the Distribution Groups a user is a member of with PowerShell in Office 365
Next Post
Resources to learn PowerShell for Office 365

8 Comments

  • January 11, 2019 at 8:18 am
    Andy

    Hi Vlad,

    Have you ever come across this using the above script?

    The operation couldn’t be performed because ” matches multiple entries.

    Thanks,
    Andy

    Reply
  • February 13, 2019 at 9:51 pm
    CK

    This did not work for me. I don’t understand why MS made this so difficult. I just want a command that shows me all the 365 Groups a user is a member of and then be able to remove the user from them.

    Reply
  • March 19, 2019 at 9:54 am
    as

    I dont get any output

    Reply
    • March 20, 2019 at 2:42 am

      Everything gets saved in the variable, try typing the variable name like in the screenshot after the loop!

      Reply
  • July 11, 2019 at 8:20 pm
    Xpyder

    If you’re having trouble, try this version:
    $mailbox = Get-Mailbox -ResultSize 1
    Get-UnifiedGroup | where { (Get-UnifiedGroupLinks $_.Alias -LinkType Members).DistinguishedName -contains $mailbox.DistinguishedName}

    Reply
  • January 24, 2020 at 3:42 pm
    Bossman69

    Thank you Xpyder, that worked for me.

    Reply
  • May 22, 2020 at 5:14 am
    ashok

    Perfect!!! work flawlessly but execution takes time if you have a large infrastructure.

    Reply
  • October 11, 2021 at 8:43 am
    baba

    Please try with this
    Get-UnifiedGroup | Where-Object { (Get-UnifiedGroupLinks $_.Alias -LinkType Members | foreach {$_.Name}) -contains “User name”}

    Reply

Leave a Reply

15 49.0138 8.38624 1 0 4000 1 https://vladtalkstech.com 300 1