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:
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
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.
-ResultSize Unlimited
Ensures all matching shared mailboxes are retrieved, not just the default number returned by 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.
| Select DisplayName, PrimarySmtpAddress, Name
Selects key properties: DisplayName (the friendly name), PrimarySmtpAddress (main email address), and Name (the unique identifier in Exchange).
@{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.
| 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:
DisplayName | PrimarySmtpAddress | Name | Manager |
Finance Shared | finance@something.com | FinanceShared | John Doe |
HR Shared | hr@something.com | HRShared | Jane 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.