Contents

Script Dumpster: List all email addresses and Mailbox Size - OnPrem & O365


Contents

I’ve optimized the script to now only query each mailbox once and have increased the speed in this customer’s environment for On-premise from 1 minute 49 seconds to 26 seconds. The Office 365 code was optimized similarly and went down from 22 minutes 14 seconds to 7 minutes 21 seconds.

The output of the optimized script is identical to the original one.

Intro

As the title suggest, I had a customer asking for a list of all email addresses within the company and the accompanying mailbox sizes.

Now this customer has a hybrid Exchange 2013/Office 365 environment, which provides some challenges. Certain On-prem cmdlets provide different output compared to their Office 365 counterparts.

Long story short, currently I have 2 scripts that will simply provide me with all of the required data.

Both pieces of code already assume that you’re connected to the Exchange/Office 365 environment with all the required permissions and cmdlets available.

On-Premise code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$Recipients = Get-Recipient -Resultsize Unlimited

$MailAccounts = $Recipients | Where-Object {$_.RecipientType -eq 'UserMailbox'}
$Others = $Recipients | Where-Object {$_.RecipientType -ne 'UserMailbox'}

$Output = @()


foreach ($MailAccount in $MailAccounts) {

    $stats = Get-MailboxStatistics -Identity $($MailAccount.DisplayName)
    $Addresses = $MailAccount | Select-Object -ExpandProperty EmailAddresses | Where-Object {$_ -is [Microsoft.Exchange.Data.SmtpProxyAddress]}
    foreach ($Address in $Addresses) {
        $props = @{'Name'=$MailAccount.Name;
               'DisplayName'=$MailAccount.DisplayName;
               'SMTPAddress'=$Address.SMTPAddress;
               'RecipientType'=$MailAccount.RecipientType;
               'MailboxSizeMB'=$stats.TotalItemSize.value.toMB()
               }
        $obj = New-Object -TypeName PSObject -Property $props
        $Output +=$obj
        }

}


foreach ($Other in $Others) {

    $props = @{'Name'=$Other.Name;
               'DisplayName'=$Other.DisplayName;
               'SMTPAddress'=$Other.SMTPAddress;
               'RecipientType'=$Other.RecipientType;
               'MailboxSizeMB'='NotAvailable'
               }
    $obj = New-Object -TypeName PSObject -Property $props
    $Output +=$obj
}

$Output | Export-Csv -NoTypeInformation -Path "$Home\desktop\AllEmailAddresses_OnPrem.csv"

Office 365 code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$Recipients = Get-Recipient -Resultsize Unlimited

$MailAccounts = $Recipients | Where-Object {$_.RecipientType -eq 'UserMailbox'}
$Others = $Recipients | Where-Object {$_.RecipientType -ne 'UserMailbox'}

$Output = @()


foreach ($MailAccount in $MailAccounts) {

    $stats = Get-MailboxStatistics -Identity $($MailAccount.DisplayName)
    $size = $stats.TotalItemSize -replace (.*\()|,| [a-z]*\), “”
    $Addresses = $MailAccount | Select-Object -ExpandProperty EmailAddresses | Where-Object {$_ -like 'smtp*'}
    foreach ($Address in $Addresses) {
        $props = @{'Name'=$MailAccount.Name;
                   'DisplayName'=$MailAccount.DisplayName;
                   'SMTPAddress'=($Address -replace 'smtp:','');
                   'RecipientType'=$MailAccount.RecipientType;
                   'MailboxSizeMB'=[math]::Round($size/1MB,0)
                   }
        $obj = New-Object -TypeName PSObject -Property $props
        $output +=$obj
    }
}


foreach ($Other in $Others) {

    $props = @{'Name'=$Other.Name;
               'DisplayName'=$Other.DisplayName;
               'SMTPAddress'=($Other -replace 'smtp:','');
               'RecipientType'=$Other.RecipientType;
               'MailboxSizeMB'='NotAvailable'
               }
    $obj = New-Object -TypeName PSObject -Property $props
    $output +=$obj
}

$Output | Export-Csv -NoTypeInformation -Path "$Home\desktop\AllEmailAddresses_O365.csv"