You could have a try with this script:
$Mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$data = @()
foreach ($Mailbox in $mailboxes){
$temp = Get-Mailbox $Mailbox.PrimarySmtpAddress | select DisplayName,WindowsEmailAddress,TotalItemSize,LastLogonTime
$temp2 = Get-MailboxStatistics $Mailbox.PrimarySmtpAddress | Select TotalItemSize,LastLogonTime
$temp.TotalItemSize = $temp2.TotalItemSize
$temp.LastLogonTime = $temp2.LastLogonTime
$data += $temp
Write-Host $Mailbox.PrimarySmtpAddress "Checked"
}
$data | Export-csv c:\temp\Result.csv -NoTypeInformation
For 5K mailboxes, I would suggest you split mailboxes into multiple parts, such as:
$Mailboxes = Get-User -RecipientTypeDetails UserMailbox -ResultSize Unlimited | where {$_.Department -eq "321"}
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.