Get a list of all mailbox permissions in Microsoft 365
I was recently asked by a colleague to help with fulfilling a customer’s request for a comprehensive list of all mailboxes in their company, including their corresponding permissions. Given its versatility in handling such tasks, I leveraged the power of PowerShell to create a script that could generate a CSV file containing the requested list of mailboxes and their respective permissions and then save the CSV file on the desktop with the organisations name in the file name. The script also outputs the permissions in the console window as it’s running.
Here’s the code:
Connect-ExchangeOnline
$Output = @()
$Mailboxes = Get-Mailbox -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes) {
Write-Host "Mailbox: $($Mailbox.PrimarySmtpAddress)" -ForegroundColor Green
$Permissions = Get-EXOMailboxPermission -Identity $Mailbox.PrimarySmtpAddress
foreach ($Permission in $Permissions) {
Write-Host " $($Permission.User.ToString()) - $($Permission.AccessRights)"
$Permission = New-Object PSObject -Property @{
Mailbox = $Mailbox.PrimarySmtpAddress
User = $($Permission.User.ToString())
Permission = "'{0}'" -f ($($Permissions[0].AccessRights) -join "'; '")
}
$Output += $Permission
}
}
$Output | Select-Object Mailbox, User, Permission | ft *
$organisation = $(Get-OrganizationConfig).Identity
$Output | Select-Object Mailbox, User, Permission | Export-Csv "$([Environment]::GetFolderPath("Desktop"))\$($organisation)_MailboxPermissions.csv" -NoTypeInformation
You might find that you need to install the latest PowerShell module for Exchange Online Management. The latest release at the time of writing is 3.1.0, so run the following command at an elevated command prompt to install.
Install-Module -Name ExchangeOnlineManagement -RequiredVersion 3.1.0