Export All On-Prem Mailboxes and Total Size using Powershell

A quick script which will export all mailboxes into a CSV, showing all email aliases, the mailbox type and and the total mailbox size in MB.

$LogPath = "C:\Temp\Mailbox-Size-Data-Advanced.csv"

#Import 2013 -> 2019 Powershell snapin
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; 

$mailboxStats = Get-MailboxDatabase | Get-MailboxStatistics

ForEach($mailbox in $mailboxStats){
    $curUserMailbox = Get-Mailbox -filter "ExchangeGuid -eq '$($mailbox.MailboxGuid)'"

    $curDisplayName = $curUserMailbox.DisplayName
    If($curDisplayName -eq $null){$curDisplayName = "[No Display Name]"}

    $curMailboxSize = $mailbox.TotalItemSize
    If($curMailboxSize -like "*,*"){$curMailboxSize = $curMailboxSize -replace ",", " "}    

    Add-Content $LogPath "$($curDisplayName),$($curUserMailbox.PrimarySmtpAddress),$($curUserMailbox.UserPrincipalName),$($curMailboxSize),$($curUserMailbox.RecipientTypeDetails)"