Share via

Getting the email addresses for shared mailboxes

mara2021 1,181 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 | Exchange Server | Management
Exchange | Exchange Server | Management

The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

  1. Aholic Liang-MSFT 13,926 Reputation points Microsoft External Staff
    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

    3 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.