Search code examples
powershellactive-directoryoffice365exchange-server

Correcting PowerShell script to show Disabled AD user account (not shared mailboxes) with Exchange mailbox and its size in MBytes?


I am attempting to modify the script below so it is showing all Disabled AD user account with Exchange User mailbox still enabled (not Shared Mailbox).

Because the script below also returns Shared Mailboxes which is always created as disabled AD user account.

$Allusers = Get-ADUser -Filter {(enabled -eq $false)} -Properties homeMDB, mailNickName, mail, DisplayName, SamAccountName, Givenname, SurName | ?{ $_.homeMDB -ne $null }
$Allusers | Select-Object Givenname, Surname, DisplayName, Mail, MailNickName, SamAccountName, homeMDB | Export-Csv "C:\DisableduserMBX.csv" -NoTypeInformation

It would be good if there is mailbox size as well in the column in MBytes.

Like in the below script:

Get-Mailbox -ResultSize Unlimited |
  Get-MailboxStatistics |
  Select DisplayName,StorageLimitStatus, `
@{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, `
ItemCount |
  Sort "TotalItemSize (MB)" -Descending

Solution

  • To add the MBYTES column, you can try this. Note this uses the filter as provided by notjustme.

    # for the sake of readability..
    $filter = '(Enabled -eq $false) -and (msExchRecipientTypeDetails -ne 4) -and ("$null" -ne homeMDB)'
    $properties = @('homeMDB', 'mailNickName', 'mail', 'DisplayName', 'SamAccountName', 'Givenname', 'SurName', 'ProxyAddresses')
    
    $Allusers = (Get-ADUser -Filter $filter -Properties $properties  |
                 ForEach-Object {
                    $size = (Get-MailboxStatistics $_.SamAccountName).TotalItemSize.Value.ToMB()
                    New-Object -TypeName PSObject -Property @{
                        homeMDB        = $_.homeMDB
                        mailNickName   = $_.mailNickName
                        mail           = $_.mail
                        ProxyAddresses = $_.ProxyAddresses -join '; '
                        DisplayName    = $_.DisplayName
                        SamAccountName = $_.SamAccountName
                        Givenname      = $_.Givenname
                        SurName        = $_.SurName
                        MBytes         = $size
                    }
                }) | Sort-Object MBytes -Descending | Export-Csv "C:\DisableduserMBX.csv" -NoTypeInformation
    

    p.s. I've added the ProxyAddresses in there to be able to spot more alias emailaddresses.

    p.s. 2 The Identity parameter for Get-MailboxStatistics can be one of:

    • Name
    • Display name
    • Alias
    • Distinguished name (DN)
    • Canonical DN
    • domain name\account name
    • Email address
    • GUID
    • LegacyExchangeDN
    • SamAccountName
    • User ID or user principal name (UPN)