Use PowerShell to get all Shared Mailboxes for a user in Exchange Online

#PowerShell #Exchange #M365

Similar to my post yesterday to get all SharePoint Sites for a user, I received another request today to generate a report of all Shared Mailboxes that a user has access to in Exchange Online.

Right now, there's no ability in the Exchange Admin center to list all Shared Mailboxes that a user can access.

To work around this limitation, I wrote a simple PowerShell script to get all Shared Mailboxes, check if the specified user has permissions, then output the report to a .csv file.

Here are the requirements to run this script:

Here's the script:

## Get-UserSharedMailboxes.ps1
## Gets all Shared Mailbox membership for a specified user, then outputs to csv.
## Requires the Exchange Administrator role and the ExchangeOnlineManagement PowerShell module.
## tdannecy@gmail.com

$userEmail = 'user@example.com'
$outputfile = 'c:\output.csv'

Import-Module -name ExchangeOnlineManagement
Connect-ExchangeOnline

$output = @()

$mailboxes = get-exomailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox

foreach ($mailbox in $mailboxes) {
    $permissions = Get-EXOMailboxPermission -identity $mailbox.Guid 
    if ($permissions) {
        foreach ($permission in $permissions) {
            if ($permission.User -eq $userEmail) {
                $curpermission = [PSCustomObject]@{
                    SharedMailboxGuid         = $mailbox.Guid
                    SharedMailboxDisplayName  = $mailbox.DisplayName
                    SharedMailboxEmailAddress = $mailbox.PrimarySmtpAddress
                    UserEmail                 = $userEmail
                    UserPermission            = ($permission.AccessRights | out-string).trim()
                } 
                $output += $curpermission
            }
                
        }
    }
}
$output | Export-Csv -Path $outputfile -NoTypeInformation
Invoke-Item -Path $outputfile

I've also uploaded this as a GitHub Gist:

Discuss...