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.
- #This script is for getting a list of disabled users that still have mailboxes
- #Execute the script with the following command
- #.\DisabledUserMailboxes.ps1 -Output C:\DisabledUserMailboxes.csv
-
- param(
- [string]$Output = $(Throw "You need to specify the output file name using -output")
- )
-
- # Get a list of users with disabled accounts, that are mail enabled
- $domain = [ADSI]""
- $searcher = new-object System.DirectoryServices.DirectorySearcher($domain)
- $searcher.filter = "(&(userAccountControl:1.2.840.113556.1.4.803:=2)(homemdb=*))"
- [Void]$Searcher.PropertiesToLoad.Add("distinguishedname")
- $searcher.PageSize = 1000
- $searcher.SearchScope = "Subtree"
- $results = $searcher.findall()
-
- write-host "$($results.count) Disabled Users are mail enabled"
-
- # Create an empty array for storing the user data
- $users = @()
-
- #Enumerate through the disabled users and make sure they are user mailboxes, placing valid objects into the $users array
- foreach($result in $results) {
- $MailboxExists = $null
-
- #Verify the user has a mailbox that is a user mailbox, not a Room, Equipment, Shared, etc mailbox
- $MailboxExists = Get-Mailbox $($result.Properties.distinguishedname).tostring() | Where-Object {($_.recipienttypedetails -eq "UserMailbox")}
-
- # 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
- if (($MailboxExists -ne "") -and ($MailboxExists -ne $null)){
- write-debug $($result.Properties.distinguishedname)
- $users += $($result.Properties.distinguishedname).tostring()
- }
-
- }
-
- # Export the list of disabled users that have Exchange mailboxes to a CSV file
- $users | select-object @{Name="DistinguishedName";Expression={$_}} | Export-Csv -Path $Output -Force -NoTypeInformation