I took this script from another Technet post, but haven't been able to get it to run correctly. I keep getting an error stating I cannot call a method on a null-valued expression.
Here is the script:
$mbxStats = @()
$Mailboxes = Get-Mailbox -ResultSize Unlimited |
ForEach-Object {
# Retrieve the identiry and store it in the variable $_
$stats = Get-MailboxStatistics -id $_
# Create a new instance of a .Net object
$mbx = New-Object System.Object
# Add user-defined customs members: the records retrieved with the three PowerShell commands
$mbx | Add-Member -MemberType NoteProperty -Value $stats.Displayname -Name DisplayName
$mbx | Add-Member -MemberType NoteProperty -Value $_.organizationalUnit -Name OrganizationalUnit
$mbx | Add-Member -MemberType NoteProperty -Value $(Get-MailboxDatabase -id $_.database).storagegroup -Name StorageGroupName
$mbx | Add-Member -MemberType NoteProperty -Value $stats.ItemCount -Name ItemCount
$mbx | Add-Member -MemberType NoteProperty -Value $stats.TotalItemSize.value.ToMB() -Name TotalSize
# Add right hand operand to value of variable ($mbx) and place result in variable ($mbxStats)
$mbxStats += $mbx
}
# Pipe the result and sort by TotalSize. Export to .csv file. Use Unicode format
$mbxStats | Sort-Object TotalSize | Export-Csv -Encoding 'Unicode' C:\Users\administrator.HOSTING\Desktop\report.csv
I'm not very fluent in Powershell so my debuggings is very limited.