To provide a list of mailboxes by size in M365 and export it to a CSV file using PowerShell, you can use the following command:
Get-MailboxStatistics -ResultSize Unlimited | Sort-Object TotalItemSize -Descending | Select-Object DisplayName, ItemCount, ` @{Name="TotalItemSize(MB)";Expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, ` StorageLimitStatus, LastLogonTime | Export-Csv -Path "C:\MailboxSizes.csv" -NoTypeInformation
This command uses the Get-MailboxStatistics cmdlet to retrieve mailbox statistics for all mailboxes in the organization, sorts the results by the TotalItemSize property in descending order, selects the DisplayName, ItemCount, TotalItemSize in MB, StorageLimitStatus, and LastLogonTime properties, and then exports the results to a CSV file located at "C:\MailboxSizes.csv".
Note that the expression used to calculate the TotalItemSize in MB converts the value from bytes to MB and rounds it to two decimal places using the Round method. The -NoTypeInformation parameter is used to exclude the object type information from the CSV file to make it easier to read.