I need to learn how to make a script that gets all users that have access to a shared mailbox in our business.
For example:
shared mailbox: [email protected]
I would like to export as a .csv like this:
| Username | Access | Mailbox |
| john.peterson | Full Access | shared_admin@business.com |
| anne.wall | Full Access | shared_admin@business.com |
| jason.doe | Full Access | shared_admin@business.com |
| tim.carlson |Full Access | shared_admin@business.com |
CodePudding user response:
First install the Exchange Online PowerShell module v2:
Install-Module -Name ExchangeOnlineManagement
Then the following code should do the job:
# Connect to Exchange Online
Connect-ExchangeOnline
# Retrieve mailbox permissions
$sharedMailbox = '[email protected]'
$permissions = Get-MailboxPermission -Identity $sharedMailbox
# Format the permissions to have the column names you asked for
$formattedPermissions = $permissions `
| Select-Object -Property @{Name='Username';Expression={$_.User}},@{Name='Access';Expression={$_.AccessRights}},@{Name='Mailbox';Expression={$sharedMailbox}}
# Export to CSV, delimiter can be adjusted
$formattedPermissions | Export-Csv -Path permissions.csv -Delimiter ',' -Encoding UTF8 -NoTypeInformation