Search code examples
powershelloffice365export-to-csv

Export shared mailbox permissions to .csv


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 | [email protected] |
| anne.wall | Full Access | [email protected] |
| jason.doe | Full Access | [email protected] |
| tim.carlson |Full Access | [email protected] |

Solution

  • 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