'Powershell: Tried to get output with Display name, lastLogonTime (3O days) and totalitemsize from Exchange

I have this pipeline:

Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox | 
    Where {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-36)} | 
    Sort -Property @{e={( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending | 
    Select-Object DisplayName,@{n="LastLogonTime";e={(Get-MailboxStatistics $_.Identity).LastLogonTime}} | 
    Get-MailboxStatistics | 
    Select-Object displayname, totalitemsize | 
    Export-Csv -Path 'C:\JV'

but the output never gets the third column in the output table - size.

DisplayName           LastLogonTime
-----------           -------------
Some User             2/8/2022 2:47:26 PM
Another Name          2/8/2022 2:46:59 PM 
Yet AnotherName       2/8/2022 2:46:54 PM


Solution 1:[1]

I believe this should be a more efficient and straight forward way of getting the data you're looking for:

$dateLimit = [datetime]::Now.AddDays(-36)

Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox, SharedMailbox | ForEach-Object {
    $stats = Get-MailboxStatistics $_.Identity
    # If the date is lower than the date limit, skip this user.
    if($stats.LastLogonTime -lt $dateLimit) { return }
    [pscustomobject]@{
        DisplayName   = $_.DisplayName
        LastLogonTime = $stats.LastLogonTime
        TotalItemSize = $stats.TotalItemSize
    }
} | Sort-Object LastLogonTime -Descending | Export-Csv -Path 'C:\path\to\csvfile.csv' -NoTypeInformation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Santiago Squarzon