# Compare mailbox count from seperate databases

**URL:** <https://forums.powershell.org/t/compare-mailbox-count-from-seperate-databases/1563>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/compare-mailbox-count-from-seperate-databases/1563 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/compare-mailbox-count-from-seperate-databases/1563/1 "2011-12-31T18:30:00Z")

</div>

by Optimal at 2013-01-30 10:07:58

> I am trying to figure out how many mailboxes are in a mailbox database. The number of mailbox databases is not static, and could be from 1 database to 50. Once I have this info, I want to compare all this data and pick the database with the fewest number of mailboxes.  
>   
> Using Auto Provision is not an option, as it is already defined to another set of mailbox databases.  
>   
> Thanks.

by Takuu at 2013-01-31 06:14:17

> This should work for you. My environment is Exchange 2007 with PSv2 but it should work in 2010. Let me know!  
>   
> `$array = @()Get-MailboxDatabase | foreach { $storagegroup = New-Object PSObject $storagegroup | Add-Member -MemberType NoteProperty -Name StorageGroup -Value $.name $tempVal = Get-Mailbox -Database $ $storagegroup | Add-Member -MemberType NoteProperty -Name MailboxCount -Value $tempVal.count $array += $storagegroup $tempVal = $null Remove-Variable storagegroup }$array | Write-Output`

by ArtB0514 at 2013-01-31 07:05:33

> Just a suggestion… With PS2+, you can save yourself a bunch of typing (and messing with temporary variables) by using the -Property property in New-Object:  
> `$array = @()Get-MailboxDatabase | foreach { $array += New-Object PSObject -Property @{ 'StorageGroup' = $.name 'MailboxCount' = (Get-Mailbox -Database $).count } }$array`

by Takuu at 2013-01-31 11:31:45

> Great suggestion ArtB0514. That does save a lot of typing there.  
> Maybe I had a typo, but when I tried to do the (Get-Mailbox -Database $_).count I was getting an error. Hence the messy $tempVal I used._  
>   
> _Thanks again!! I’ll be using this format for objects from now on._

_by Optimal at 2013-01-31 12:28:32_

> _Thank you ArtB and Takuu. The solutions weren’t quite what I was looking for, but they allowed me to find what I needed. The $dbs var is user defined. Also, just a note on ArtB’s solution: You must add .name to (Get-Mailbox -Database $_).count, otherwise you will have errors. ((Get-Mailbox -Database $_.name).count)_  
>   
> _Thanks again!!!_  
>   
> _`$dbs = ("SERVICE1;SERVICE2;SERVICE3").split(";")$array = @()$dbs | foreach { $array += New-Object PSObject -Property @{ 'StorageGroup' = $`_` 'MailboxCount' = (Get-Mailbox -Database $_).count } }$database = ($array | sort MailboxCount â€“Descending)[0].StorageGroup$database`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:03pm UTC](https://forums.powershell.org/t/compare-mailbox-count-from-seperate-databases/1563/2 "2024-05-16T21:03:35Z")

</div>


