locked
Disabled AD users with a mailbox in Exchange 2010 RRS feed

  • Question

  • Hi,

    I am looking to run a script to find out what disabled AD users have a mailbox in Exchange 2010 SP3.

    I found the following but before I run it can someone confirm it will do as I expect as I don't have a test lab and I am limited to scripts I can run due to my company's business nature.

    Thanks 

    get-Mailbox -resultsize unlimited | where { $_.ExchangeUserAccountControl -match “AccountDisabled”} | select name,database,organizationalunit | sort-object name | export-csv c:\admin\disabledwithmail.csv


    Thursday, July 3, 2014 3:27 PM

Answers

  • Oh boy... that's tough with Exchange 2010 and powershell 2 Export-Mailbox append switch limitation.... but here is the script just for you...

    $MBXs = get-Mailbox -resultsize unlimited
    $Final = New-Object PsObject
    $Final = @()
    foreach ($MBX in $MBXs) 
    {   if((Get-User $MBX).UserAccountControl -like "*AccountDisabled*")
    	{
    	$Good = New-Object PsObject
       	$EachEntry = Get-User $mbx | select name,organizationalunit, @{expression={(get-mailboxStatistics $mbx.displayname).totalitemsize.value.ToMB()};label="Size(MB)"}
    	$Good | Add-Member NoteProperty name $EachEntry.name
    	$Good | Add-Member NoteProperty organizationalunit $EachEntry.organizationalunit
    	$Good | Add-Member NoteProperty "Size(MB)" $EachEntry."Size(MB)"
    	
    $Final += $Good
    $Good = @()
    }
    }
    $Final | Export-Csv DisabledUsers-output.csv

    Blog | Get Your Exchange Powershell Tip of the Day from here


    • Edited by Amit Tank Friday, July 4, 2014 2:12 PM
    • Proposed as answer by Zaza Abramov Saturday, July 5, 2014 7:08 AM
    • Marked as answer by onthejobagain Tuesday, July 8, 2014 1:04 PM
    Friday, July 4, 2014 2:12 PM

All replies

  • There won't be any impact on running Get-* cmdlet in production environment, that gives you just information and doesn't change anything...

    This will give you better result though...

    get-Mailbox -resultsize unlimited | Get-User | where { $_.UserAccountControl -match “AccountDisabled”} | select name,organizationalunit | sort-object name | export-csv c:\admin\disabledwithmail.csv


    Blog | Get Your Exchange Powershell Tip of the Day from here

    • Proposed as answer by Zaza Abramov Saturday, July 5, 2014 7:07 AM
    Thursday, July 3, 2014 6:37 PM
  • Hi,

    I have some tests in my environment using Exchange 2010. I recommend you use the cmdlet Amit provided to achieve your goal. Here is the reason why I recommend you to do so:

    If I disable an AD user amy but don't disable the mailbox, when I run the Get-Mailbox amy |fl ExchangeUserAccountControl, the value can't change to "AccountDisabled" immediately, but when I run Get-User amy |fl UserAccountControl, the value of UserAccountControl can change to "AccountDisabled, NormalAccount" immediately.

    Hope my clarification is helpful.

    Best regards,


    Amy Wang
    TechNet Community Support

    Friday, July 4, 2014 9:17 AM
  • Thanks Amit, worked a treat.

    One more thing, if I wanted to get the size of these mailboxes what syntax would I need to add to me current script ?

    Friday, July 4, 2014 9:56 AM
  • Oh boy... that's tough with Exchange 2010 and powershell 2 Export-Mailbox append switch limitation.... but here is the script just for you...

    $MBXs = get-Mailbox -resultsize unlimited
    $Final = New-Object PsObject
    $Final = @()
    foreach ($MBX in $MBXs) 
    {   if((Get-User $MBX).UserAccountControl -like "*AccountDisabled*")
    	{
    	$Good = New-Object PsObject
       	$EachEntry = Get-User $mbx | select name,organizationalunit, @{expression={(get-mailboxStatistics $mbx.displayname).totalitemsize.value.ToMB()};label="Size(MB)"}
    	$Good | Add-Member NoteProperty name $EachEntry.name
    	$Good | Add-Member NoteProperty organizationalunit $EachEntry.organizationalunit
    	$Good | Add-Member NoteProperty "Size(MB)" $EachEntry."Size(MB)"
    	
    $Final += $Good
    $Good = @()
    }
    }
    $Final | Export-Csv DisabledUsers-output.csv

    Blog | Get Your Exchange Powershell Tip of the Day from here


    • Edited by Amit Tank Friday, July 4, 2014 2:12 PM
    • Proposed as answer by Zaza Abramov Saturday, July 5, 2014 7:08 AM
    • Marked as answer by onthejobagain Tuesday, July 8, 2014 1:04 PM
    Friday, July 4, 2014 2:12 PM
  • Don't use "where" when you can use a filter and offload the selection to LDAP. In other words, rephrase your question to "what mailboxes belong to disabled AD users". :-)

    This is much simpler and it won't return every mailbox in the organization (an important consideration in a larger organization):

    get-mailbox -resultsize unlimited -filter "ExchangeUserAccountControl -eq 'AccountDisabled'" |  . . .


    --- Rich Matheisen MCSE&I, Exchange MVP

    Sunday, July 6, 2014 9:36 PM
  • And an even better result if you use "-filter" instead of "where". :-)

    --- Rich Matheisen MCSE&I, Exchange MVP

    Sunday, July 6, 2014 9:37 PM
  • thanks for all the help
    Tuesday, July 8, 2014 1:04 PM