Mailboxes Associated with Disabled Users

Periodically you may wish to identify mailboxes that are still active in Exchange 2007 but which are not associated with active AD users.  As you are aware, some mailboxes within Exchange require disabled accounts, so you generally want to exclude those mailboxes from such reports.  Below is a script that I put together to produce a report of disabled user’s mailboxes.

I prefer not to use external utilities when possible, as it makes my scripts more portable in nature, which is why you’ll see the use of ADSI instead of the Quest cmdlets.  This script does require the Exchange PSSnapIns, so you’ll want to execute this within the EMS.

  1. #This script is for getting a list of disabled users that still have mailboxes
  2. #Execute the script with the following command
  3. #.\DisabledUserMailboxes.ps1 -Output C:\DisabledUserMailboxes.csv
  4.  
  5. param(
  6. [string]$Output = $(Throw "You need to specify the output file name using -output")
  7. )
  8.  
  9. # Get a list of users with disabled accounts, that are mail enabled
  10. $domain = [ADSI]""
  11. $searcher = new-object System.DirectoryServices.DirectorySearcher($domain)
  12. $searcher.filter = "(&(userAccountControl:1.2.840.113556.1.4.803:=2)(homemdb=*))"
  13. [Void]$Searcher.PropertiesToLoad.Add("distinguishedname")
  14. $searcher.PageSize = 1000
  15. $searcher.SearchScope = "Subtree"
  16. $results = $searcher.findall()
  17.  
  18. write-host "$($results.count) Disabled Users are mail enabled"
  19.  
  20. # Create an empty array for storing the user data
  21. $users = @()
  22.  
  23. #Enumerate through the disabled users and make sure they are user mailboxes, placing valid objects into the $users array
  24. foreach($result in $results) {
  25. $MailboxExists = $null
  26.  
  27. #Verify the user has a mailbox that is a user mailbox, not a Room, Equipment, Shared, etc mailbox
  28. $MailboxExists = Get-Mailbox $($result.Properties.distinguishedname).tostring() | Where-Object {($_.recipienttypedetails -eq "UserMailbox")}
  29.  
  30. # Make sure the result is neither an emtpy string nor a null value, if not add the current user to the list of disabled mailbox users
  31. if (($MailboxExists -ne "") -and ($MailboxExists -ne $null)){
  32. write-debug $($result.Properties.distinguishedname)
  33. $users += $($result.Properties.distinguishedname).tostring()
  34. }
  35.  
  36. }
  37.  
  38. # Export the list of disabled users that have Exchange mailboxes to a CSV file
  39. $users | select-object @{Name="DistinguishedName";Expression={$_}} | Export-Csv -Path $Output -Force -NoTypeInformation
This entry was posted in Active Directory, Exchange 2007, PowerShell. Bookmark the permalink.