The script creates a CSV file containing a list of disabled mailboxes and their relevant information. It works in on-premises Exchange and Exchange Online. The script fetches user mailboxes from Exchange, filters them to select only “UserMailbox” types, checks if the associated AD user accounts are disabled, gathers statistics for disabled mailboxes, sorts them based on the size of their items, and exports the list to a CSV file. This could be used, for example, to identify and manage disabled mailboxes and their corresponding sizes.
The provided script performs the following tasks:
- Retrieves a list of user mailboxes with no result size limit using the `Get-Mailbox` cmdlet. It then filters the mailboxes using a `Where-Object` clause, selecting only those with `RecipientTypeDetails` equal to “UserMailbox”. The filtered mailboxes are stored in the variable `$Mailboxes`.
- Initializes an empty array `$Disabled` to store information about disabled mailboxes.
- Iterates through each mailbox in the `$Mailboxes` array using a `ForEach` loop, where each mailbox is represented by the variable `$Mailbox`.
- Inside the loop, it checks if the corresponding Active Directory user account associated with the mailbox (retrieved using `Get-ADUser`) is disabled. If the user account is disabled (enabled equals `$False`), it executes the following steps:
- Retrieves mailbox statistics for the current mailbox using the `Get-MailboxStatistics` cmdlet and selects the properties “DisplayName” and “TotalItemSize”.
- The resulting object with DisplayName and TotalItemSize properties is appended to the `$Disabled` array.
- After the loop completes, the script has collected information about all disabled mailboxes and stored it in the `$Disabled` array.
- The script then sorts the contents of the `$Disabled` array in descending order based on the “TotalItemSize” property using the `Sort-Object` cmdlet.
- Exports the sorted list of disabled mailboxes to a CSV file named “DisabledList.csv” located in the c:temp directory using the `Export-Csv` cmdlet. The `-NoTypeInformation` parameter ensures that the CSV file does not include the data type information in the header row.