none
How to list Disabled AD account Exchange mailboxes that is not Shared mailbox along with the Size in MBytes ? RRS feed

  • Question

  • Hi All,

    I need someone to assist me in modifying 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

    Thanks before for your help. 


    /* Server Support Specialist */


    Friday, August 31, 2018 8:04 AM

Answers

  • This should get you what you want, as always test in lab first.

    $report = @()

    # refer to https://support.microsoft.com/en-ca/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
    # for the useraccountcontrol flag '546' it is the total of ACCOUNTDISABLE,PASSWD_NOTREQD,NORMAL_ACCOUNT
     
    $Users = Get-user -ResultSize Unlimited -Filter {(UserAccountControl -eq '546') -AND (recipientType -eq 'UserMailbox')}

        $MBCounter=0
        $MBTotal=@($Users).count


        foreach ($user in $Users)
        {
        $Pct=($MBCounter/$MBTotal)*100    
        Write-Progress -Activity "Processing Users" -status "Mailbox $MBcounter of $MBTotal" -percentcomplete $pct

    #$mailboxInfo = Get-MailboxStatistics $User | Select StorageLimitStatus,@{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}
    $mailboxInfo = Get-MailboxStatistics $User | Select StorageLimitStatus,TotalItemSize


    $mbObj = New-Object PSObject
            $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $User.DisplayName
            $mbObj | Add-Member -MemberType NoteProperty -Name "First Name" -Value $User.FirstName
            $mbObj | Add-Member -MemberType NoteProperty -Name "Last Name" -Value $User.lastName
    $mbObj | Add-Member -MemberType NoteProperty -Name "StorageLimitStatus" -Value $mailboxInfo.StorageLimitStatus
    $ItemSize = [math]::Round(($mailboxInfo.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
    $mbObj | Add-Member -MemberType NoteProperty -Name "TotalItemSize" -Value $ItemSize
    $report += $mbObj
            $MBCounter++
        }
    $report | Export-csv -notypeinformation C:\temp\DisabledUserwithMailbox.csv

    Friday, August 31, 2018 3:58 PM
  • Hi Senior System Engineer,

    I think if you change the order in your script, it will be more easy: Find out UserMailbox first, then check whether its' AD account is disabled:

    $Mailboxes = Get-Mailbox | where {$_.RecipientTypeDetails -eq "UserMailbox"}
    $Disabled = @()
    
    Foreach ($Mailbox in $Mailboxes) {
        if((Get-ADUser $Mailbox.Name).Enabled -eq $False){
            $Disabled += Get-MailboxStatistics $Mailbox.Name | Select DisplayName,TotalItemSize
        }    
    }
    $Disabled | Export-Csv c:/temp/DisabledList.csv -NoTypeInformation

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, September 3, 2018 2:40 AM
    Moderator

All replies

  • This should get you what you want, as always test in lab first.

    $report = @()

    # refer to https://support.microsoft.com/en-ca/help/305144/how-to-use-the-useraccountcontrol-flags-to-manipulate-user-account-pro
    # for the useraccountcontrol flag '546' it is the total of ACCOUNTDISABLE,PASSWD_NOTREQD,NORMAL_ACCOUNT
     
    $Users = Get-user -ResultSize Unlimited -Filter {(UserAccountControl -eq '546') -AND (recipientType -eq 'UserMailbox')}

        $MBCounter=0
        $MBTotal=@($Users).count


        foreach ($user in $Users)
        {
        $Pct=($MBCounter/$MBTotal)*100    
        Write-Progress -Activity "Processing Users" -status "Mailbox $MBcounter of $MBTotal" -percentcomplete $pct

    #$mailboxInfo = Get-MailboxStatistics $User | Select StorageLimitStatus,@{name="TotalItemSize (MB)"; expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}
    $mailboxInfo = Get-MailboxStatistics $User | Select StorageLimitStatus,TotalItemSize


    $mbObj = New-Object PSObject
            $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $User.DisplayName
            $mbObj | Add-Member -MemberType NoteProperty -Name "First Name" -Value $User.FirstName
            $mbObj | Add-Member -MemberType NoteProperty -Name "Last Name" -Value $User.lastName
    $mbObj | Add-Member -MemberType NoteProperty -Name "StorageLimitStatus" -Value $mailboxInfo.StorageLimitStatus
    $ItemSize = [math]::Round(($mailboxInfo.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
    $mbObj | Add-Member -MemberType NoteProperty -Name "TotalItemSize" -Value $ItemSize
    $report += $mbObj
            $MBCounter++
        }
    $report | Export-csv -notypeinformation C:\temp\DisabledUserwithMailbox.csv

    Friday, August 31, 2018 3:58 PM
  • Hi Senior System Engineer,

    I think if you change the order in your script, it will be more easy: Find out UserMailbox first, then check whether its' AD account is disabled:

    $Mailboxes = Get-Mailbox | where {$_.RecipientTypeDetails -eq "UserMailbox"}
    $Disabled = @()
    
    Foreach ($Mailbox in $Mailboxes) {
        if((Get-ADUser $Mailbox.Name).Enabled -eq $False){
            $Disabled += Get-MailboxStatistics $Mailbox.Name | Select DisplayName,TotalItemSize
        }    
    }
    $Disabled | Export-Csv c:/temp/DisabledList.csv -NoTypeInformation

    Regards,

    Kyle Xu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, September 3, 2018 2:40 AM
    Moderator
  • Many thank you for the suggestion and the corrections :-)

    I appreciate it muchly.


    /* Server Support Specialist */

    Monday, September 3, 2018 2:54 AM
  • This worked for me, using SamAccountName instead of Name

    ------------------------------------------------------------------------


    $Mailboxes = Get-Mailbox -ResultSize 'unlimited' | where {$_.RecipientTypeDetails -eq "UserMailbox"}
    $Disabled = @()

    Foreach ($Mailbox in $Mailboxes) {if((Get-ADUser $Mailbox.SamAccountName).Enabled -eq $False){$Disabled += Get-MailboxStatistics $Mailbox.Name | Select DisplayName,TotalItemSize}} $Disabled | Sort-Object TotalItemSize –Descending | Export-Csv c:/temp/DisabledList.csv -NoTypeInformation



    • Edited by Adam.ProInteg Thursday, July 18, 2019 11:10 PM Added Sorting
    Thursday, July 18, 2019 10:49 PM