I want to find all users on a particular storage group...

Continuing with my trend of turning questions I’ve been asked into blog posts, here’s another good one I was recently asked…

“I want to find all users on a particular storage group. Mailbox objects have a database field, and databases have a StorageGroupName. I wanted to do:
Get-Mailbox | where { $_.Database.StorageGroupName -eq ‘First Storage Group’ }

But that didn’t work.”

Excellent question! The data here is nested one level deeper than this person was able to get to easily. They weren’t able to figure out a way to drill directly into the Database property on the mailbox to get to the StorageGroupName (and, initially, neither was I… but more on this in the next post).

There are a bunch of ways to accomplish this particular task in Exchange 2007. Let me walk you though the thoughts I had as I came up with an answer for them.

My first way of doing it: This sort of iteration is probably terribly non-performant at high scale, but you could nest it just like the above logic expresses the problem with a one-liner like this:

Get-Mailbox | where { (Get-MailboxDatabase $_.Database).StorageGroup.Name -eq ‘First Storage Group’ }

That would totally work, but if you have a ton of mailboxes within your scope, it’s probably going to be fairly slow since it has to get the mailbox databases for each mailbox you pipe in.

So, since you probably have a lot more mailboxes than mailbox databases, a second way to do it is to turn the loop inside out with a one-liner like this:

Get-MailboxDatabase | where { $_.StorageGroup.Name -eq ‘First Storage Group’ } | foreach { Get-Mailbox -Database $_.Name }

Of course, after all this thinking, there was also a third way to do it. This one is probably the fastest one of the three, since it should be server-side filtering and makes use of some clever wildcarding. It just requires some knowledge about how the “Database” property is constructed on mailboxes:

Get-Mailbox -Database ‘*\First Storage Group\*’

I agree, the third one almost feels like cheating it’s so simple for this particular question! But hopefully the first two examples will be useful to you if you’re trying to get this sort of filtered data output for some other nested properties.