Getting the email addresses for shared mailboxes

mara2021 1,036 Reputation points
2023-03-06T19:03:50.91+00:00

I am able to get the shared mailbox and permissions using the PowerShell script below. How would I add the shared mailbox email address and alias to the script also. Thank you for your help.

Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited
     |Get-MailboxPermission |Select-Object Identity,User,{$_.AccessRights}
     | Where-Object {($_.user -like 'domain*')} |Export-Csv e:\sharedMBX.csv -NoTypeInformation
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,380 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,132 questions
0 comments No comments
{count} votes

Accepted answer
  1. Aholic Liang-MSFT 13,821 Reputation points Microsoft Vendor
    2023-03-07T09:05:13.44+00:00

    Hi @ mara2021 ,

    The following script can achieve your requirements, please modify "domain" and run it:

    # Get information about all shared mailboxes
    $SharedMailboxes = Get-Mailbox -Filter {RecipientTypeDetails -eq "SharedMailbox"}|Select-Object Identity,PrimarySmtpAddress,Alias
    
    # Loop through each shared mailbox
    $Data = foreach ($SharedMailbox in $SharedMailboxes) {
    
       
        $Users = Get-MailboxPermission -Identity $SharedMailbox | Select-Object  User,AccessRights | Where-Object {($_.user -like    '*domain*')}
        foreach($user in $Users){
       
    
        [PSCustomObject]@{
                "Identity" = $SharedMailbox.Identity   
                "EmailAddress" = $SharedMailbox.PrimarySmtpAddress   
                "Alias" = $SharedMailbox.Alias   
                "User" = $user.user
                "Permission" = $user.AccessRights
            }
        }
        
    }
    
    # Export data to CSV file
    $Data | Export-Csv -Path "c:\SharedMailboxes.csv" -NoTypeInformation
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,111 Reputation points
    2023-03-06T19:35:06.34+00:00

    You can use something like this:

    Get-Mailbox -RecipientTypeDetails SharedMailbox -ResultSize:Unlimited |
        ForEach-Object{
            if ($_.user -like 'domain*'){
                $props = [ordered]@{
                    EmailAddress = $_.emailaddress
                    Alias = $_.Alias
                    Identity = ""
                    User = ""
                    AccessRights = ""
                }
                $m =  $_ | Get-MailboxPermission | Select-Object Identity,User,{$_.AccessRights}
                $props.Identity = $m.Identity
                $props.User = $m.User
                $props.AccssRights = $m | Select-Object -Expand AccessRights
                [PSCustomObject]$props
            } 
        } |Export-Csv e:\sharedMBX.csv -NoTypeInformation
    
    

    I don't have access to an Exchange organization so I'm not sure that the property names are correct (I used the ones in your code).

    I do know that you are not going to be able to export the AccessRights to a CSV without further processing them, because there's no way to add an array to a CSV. I'm also pretty sure that an individual access right isn't just a simple name. It might be, for example, an "allow" or "deny" permission.