PowerShell Efficiencies

I have been watching Don’s PowerShell nuggets on CBT Nugget and I know there has to be a more efficient way to get this info. It currently takes about 60 seconds to work through 975 mailboxes. I would actually prefer a nudge in the right direction (i.e. what help files to look at) instead of a full solution. I took this a full solution from somewhere and I am struggling to fully understand it. I am hoping that with a little guidance I can fully understand what is happening and be able to grow from it.

Get-Mailbox -RecipientTypeDetails UserMailbox| Get-MailboxStatistics | where {$.TotalItemSize -gt 1073741824}| sort-object TotalItemSize -descending |
Add-Member -MemberType ScriptProperty -Name TotalItemSizeinMB -Value {$this.totalitemsize.value.ToMB()} -PassThru |
Select @{Expression={$
.DisplayName};Label=“User”}, @{Expression={$.TotalItemSizeinMB};Label=“Size (MB)”}, @{Expression={"{0:P0}" -f ($.TotalItemSizeinMB/1024)};Label=“Amount Over 1 GB”}


A minute for almost 1000 mailboxes? Not bad. The biggest delay is probably (a) filtering through them all to eliminate ones that aren’t over your size threshold, and then (b) sorting what’s left. No way to speed up either operation, really.

That said, your Add-Member is probably unnecessary. You could add that same TotalItemSizeInMB property by just using Select-Object, although you’d add a NoteProperty with a static value, rather than a ScriptProperty that has to execute code every time you access the property. You’re already adding custom properties for “User” and “Size(MB)”, so I’m not sure if the “TotalItemSizeInMB” gives you any value-add.

But you’re asking PowerShell to do a lot of work… 60 seconds isn’t bad.

Thanks for the feedback Don. I will just leave it be and move on. Thanks again.