Get Remote Shared Mailbox using LDAPFilter

Remote Shared Mailbox

Find out how to get Remote Shared Mailbox using LDAPFilter. In this short article, I wanted to share a PowerShell script for getting user objects where property msExchRecipientTypeDetails is a Remote Shared Mailbox.

Recipient Type Values

First, we need to find which property and value are for Remote Shared Mailboxes. There are three attributes that we can check:

  1. msExchRemoteRecipientType
  2. msExchRecipientDisplayType
  3. msExchRecipientTypeDetails

In this case, we will focus on msExchRecipientTypeDetails attribute and value 34359738368:

1UserMailbox
2LinkedMailbox
4SharedMailbox
16RoomMailbox
32EquipmentMailbox
128MailUser
2147483648RemoteUserMailbox
8589934592RemoteRoomMailbox
17179869184RemoteEquipmentMailbox
34359738368RemoteSharedMailbox

To get users that match the above criteria we can run this simple one-liner script which uses ActiveDirectory module command – Get-ADUser:

Get-ADUser

For more details about those attributes please visit the Microsoft knowledge base page.

Remote Shared Mailbox

Below you can find final script that executes the following steps:

  • Check if the ActiveDirectory module is installed on your machine.
  • Prepare parameters that will be used in this script.
  • Run Get-ADUser command with LDAPFilter.
  • Translate each property to a readable format using switch.
  • Export results to CSV file on your desktop.
  • Import results and open in a new window.
        #Import Modules ##########################################################         
        Try{
            Import-Module ActiveDirectory -ErrorAction Stop
        }
        Catch{
            Write-Warning $_.Exception.Message
            Read-Host "Script will end. Press enter to close the window"
            Exit
        }
 
 
        #Params ##################################################################
        $Recipient   = '34359738368'
        $ReportPath  = "$env:userprofile\desktop\"
        $FileDate    = Get-Date -Format "yyyyMMddHHmmss"
        $OutputCsv   = "$ReportPath\SharedMailboxes_$FileDate.csv"  
 
 
        #Properties ############################################################## 
        $Params = @{
            LDAPFilter   = "(msExchRecipientTypeDetails=$Recipient)"
            Server       = ($env:LOGONSERVER -replace "\\",'')
            Properties   = 'Name',
                           'Enabled',
                           'WhenCreated',
                           'msExchRemoteRecipientType',
                           'msExchRecipientDisplayType',
                           'msExchRecipientTypeDetails'        
        }
 
 
        #Get users ###############################################################
        Get-ADUser @Params | foreach {
                    $ExchR = $ExchRD  = $ExchRTD = $null
 
                    $ExchR = Switch ($($_.'msExchRemoteRecipientType')){
                        {$_ -eq '1'}     {'ProvisionMailbox'}
                        {$_ -eq '2'}     {'ProvisionArchive (On-Prem Mailbox)'}
                        {$_ -eq '3'}     {'ProvisionMailbox, ProvisionArchive'}
                        {$_ -eq '4'}     {'Migrated (UserMailbox)'}
                        {$_ -eq '6'}     {'ProvisionArchive, Migrated'}
                        {$_ -eq '8'}     {'DeprovisionMailbox'}
                        {$_ -eq '10'}    {'ProvisionArchive, DeprovisionMailbox'}
                        {$_ -eq '16'}    {'DeprovisionArchive (On-Prem Mailbox)'}
                        {$_ -eq '17'}    {'ProvisionMailbox, DeprovisionArchive'}
                        {$_ -eq '20'}    {'Migrated, DeprovisionArchive'}
                        {$_ -eq '24'}    {'DeprovisionMailbox, DeprovisionArchive'}
                        {$_ -eq '33'}    {'ProvisionMailbox, RoomMailbox'}
                        {$_ -eq '35'}    {'ProvisionMailbox, ProvisionArchive, RoomMailbox'}
                        {$_ -eq '36'}    {'Migrated, RoomMailbox'}
                        {$_ -eq '38'}    {'ProvisionArchive, Migrated, RoomMailbox'}
                        {$_ -eq '49'}    {'ProvisionMailbox, DeprovisionArchive, RoomMailbox'}
                        {$_ -eq '52'}    {'Migrated, DeprovisionArchive, RoomMailbox'}
                        {$_ -eq '65'}    {'ProvisionMailbox, EquipmentMailbox'}
                        {$_ -eq '67'}    {'ProvisionMailbox, ProvisionArchive, EquipmentMailbox'}
                        {$_ -eq '68'}    {'Migrated, EquipmentMailbox'}
                        {$_ -eq '70'}    {'ProvisionArchive, Migrated, EquipmentMailbox'}
                        {$_ -eq '81'}    {'ProvisionMailbox, DeprovisionArchive, EquipmentMailbox'}
                        {$_ -eq '84'}    {'Migrated, DeprovisionArchive, EquipmentMailbox'}
                        {$_ -eq '100'}   {'Migrated, SharedMailbox'}
                        {$_ -eq '102'}   {'ProvisionArchive, Migrated, SharedMailbox'}
                        {$_ -eq '116'}   {'Migrated, DeprovisionArchive, SharedMailbox'}
                        default {' - '}
                    }
 
                    $ExchRD = Switch ($($_.'msExchRecipientDisplayType')){
                        {$_ -eq '-2147483642'}  {'MailUser (RemoteUserMailbox)'}
                        {$_ -eq '-2147481850'}  {'MailUser (RemoteRoomMailbox)'}
                        {$_ -eq '-2147481594'}  {'MailUser (RemoteEquipmentMailbox)'}
                        {$_ -eq '0'}            {'UserMailbox (shared)'}
                        {$_ -eq '1'}            {'MailUniversalDistributionGroup'}
                        {$_ -eq '6'}            {'MailContact'}
                        {$_ -eq '7'}            {'UserMailbox (room)'}
                        {$_ -eq '8'}            {'UserMailbox (equipment)'}
                        {$_ -eq '1073741824'}   {'UserMailbox'}
                        {$_ -eq '1073741833'}   {'MailUniversalSecurityGroup'}
                        default {' - '}
                    }
 
                    $ExchRTD = Switch ($($_.'msExchRecipientTypeDetails')){
                        {$_ -eq '1'}            {'UserMailbox'}
                        {$_ -eq '2'}            {'LinkedMailbox'}
                        {$_ -eq '4'}            {'SharedMailbox'}
                        {$_ -eq '16'}           {'RoomMailbox'}
                        {$_ -eq '32'}           {'EquipmentMailbox'}
                        {$_ -eq '128'}          {'MailUser'}
                        {$_ -eq '2147483648'}   {'RemoteUserMailbox'}
                        {$_ -eq '8589934592'}   {'RemoteRoomMailbox'}
                        {$_ -eq '17179869184'}  {'RemoteEquipmentMailbox'}
                        {$_ -eq '34359738368'}  {'RemoteSharedMailbox'}
                        default {' - '}
                    }
 
                    $Object = New-Object PSCustomObject
                    $Object | Add-Member -MemberType NoteProperty -Name "Username"             -Value $($_.name)
                    $Object | Add-Member -MemberType NoteProperty -Name "WhenCreated"          -Value $($_.whenCreated)
                    $Object | Add-Member -MemberType NoteProperty -Name "RemoteRecipientType"  -Value $($ExchR)
                    $Object | Add-Member -MemberType NoteProperty -Name "RecipientDisplayType" -Value $($ExchRD)
                    $Object | Add-Member -MemberType NoteProperty -Name "RecipientTypeDetails" -Value $($ExchRTD)
                    $Object 
 
        } | Export-Csv $OutputCsv -NoTypeInformation  
 
 
        #Import CSV and display results ##########################################
        Import-CSV $OutputCsv | Out-GridView -Title 'SharedMailboxes'
 

I hope that this was informative for you 🙂 See you in the next articles.

2 thoughts on “Get Remote Shared Mailbox using LDAPFilter

  1. Hi Pawel,

    Clean script and useful … as usual 🙂

    I have a small question : Why feed your PSCustomObject with this old legacy way ? Any Reason ? Why not like the following ?
    $object =[PSCustomObject]@{
    Username = $($_.name)
    WhenCreated = $($_.whenCreated)
    RemoteRecipientType = $($ExchR)
    RecipientDisplayType = $($ExchRD)
    RecipientTypeDetails = $($ExchRTD)
    }

    Regards
    Olivier

    1. Hi Olivier,
      Thanks 🙂 I’m creating custom objects using multiple ways. I’m also using your way sometimes and I’m adding ordered option there as well.

      New-Object PSObject -Property ([ordered]@{
              
                  User              = $UserDetails.’foreignname’
                  Description       = $UserDetails.Description
                  Mail              = $UserDetails.mail
                  UserType          = $Code
                          
          })

      As you could saw in old articles I sometimes create an object even easier, like:

      $Object = @{} | Select TimeCreated,
      EventID,
      OperationType,
      AccountName,
      DirectoryServiceName,
      ObjectDN,
      ObjectClass,
      AttributeLDAPName,
      AttributeValue
       
      $Object.TimeCreated = $Event.TimeCreated
      $Object.EventID = $Event.ID
      $Object.OperationType = $Operation
      $Object.AccountName = $Event.Properties[3].Value
      $Object.DirectoryServiceName = $Event.Properties[6].Value
      $Object.ObjectDN = $Event.Properties[8].Value
      $Object.ObjectClass = $Event.Properties[10].Value
      $Object.AttributeLDAPName = $Event.Properties[11].Value
      $Object.AttributeValue = $Event.Properties[13].Value
      $Object

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.