How to find Shared Mailboxes by Domain and List Their Managers

How to find Shared Mailboxes by Domain and List Their Managers

Introduction

In this blog post, we’ll walk through a powerful PowerShell script that helps Microsoft 365 administrators extract details about shared mailboxes containing a specific domain in their email addresses. We’ll also find out who the manager is for each mailbox. This is incredibly helpful for documentation, audits, or managing permissions more effectively.

PowerShell Script

Below is the complete PowerShell script used to get shared mailboxes that contain ‘@something.com’ in their email addresses, along with the name of their respective manager. The results are then exported to a CSV file:

PowerShell
Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited -Filter { EmailAddresses -like "*@something.com" } |<br>Select DisplayName, PrimarySmtpAddress, Name, @{n='Manager';e={(Get-User $_.name).manager}} |<br>Export-Csv "C:\Users\Desktop\Information.csv" -NoTypeInformation

Detailed Script Explanation

PowerShell
Get-Mailbox -RecipientTypeDetails SharedMailbox

This command retrieves all mailboxes of the type ‘SharedMailbox’. Shared mailboxes are used by multiple users to read and send email from a common account.

PowerShell
-ResultSize Unlimited

Ensures all matching shared mailboxes are retrieved, not just the default number returned by PowerShell.

PowerShell
-Filter { EmailAddresses -like '*@something.com' }

Filters the mailboxes to only include those that have email addresses containing ‘@something.com’. This is useful if you’re targeting a specific domain.

PowerShell
| Select DisplayName, PrimarySmtpAddress, Name

Selects key properties: DisplayName (the friendly name), PrimarySmtpAddress (main email address), and Name (the unique identifier in Exchange).

PowerShell
@{n='Manager';e={(Get-User $_.name).manager}}

Adds a custom column ‘Manager’ by fetching the manager of the user associated with the mailbox using Get-User.

PowerShell
| Export-Csv 'C:\Users\Desktop\Information.csv' -NoTypeInformation

Exports the final output to a CSV file at the given path. ‘-NoTypeInformation’ omits the type metadata from the CSV.

Sample Output

Here’s what the CSV file might look like after you run the script:

DisplayNamePrimarySmtpAddressNameManager
Finance Sharedfinance@something.comFinanceSharedJohn Doe
HR Sharedhr@something.comHRSharedJane Smith

Tips and Best Practices

– Always run this script in Exchange Online PowerShell for best results.

– Use domain-specific filters to target only relevant shared mailboxes.

– Make sure you have the right permissions to fetch user and manager details.

Conclusion

This PowerShell script is a quick and effective way to extract shared mailbox data filtered by domain and enriched with managerial information. It’s perfect for reporting, security audits, or just understanding how shared mailboxes are structured in your environment.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *