Count Powershell output
Question – How many exchange servers in your organization? Easy to count?Ok, then how many Equipment and room mailboxes in your organization? Well, you know how to get the list but how to count them real quick?
This is a quick powershell tip. Let’s take an example
The cmdlet: Get-ExchangeServer will return the list of all exchange servers in the organization.
Now try this:
[PS] C:\Documents and Settings\ratish>Get-ExchangeServer | measure-object
Count : 62
Average :
Sum :
Maximum :
Minimum :
Property :
Now, this includes all of them – E2K, E2K3, E2K7 all of them… You could distinguish by looking at the AdminDisplayVersion value.
Let’s take another example:
This one returns all Equipment mailboxes:
Get-Mailbox -ResultSize Unlimited | where {$_.ResourceType -eq “Equipment”}
If you wanna count it, add the Measure-Object switch.
Get-Mailbox -ResultSize Unlimited | where {$_.ResourceType -eq “Equipment”} | measure-object
Another one perhaps:
This one returns the count of all mailboxes on a specific server:
Get-Mailbox -ResultSize Unlimited | where {$_.ServerName -eq “SERVERNAME”} | measure-object
Now, remember that the “Measure-Object” switch is something which counts the lines in the output and gives results.
Ratish Nair
MVP Exchange
Team @MSExchangeGuru
Keywords: Count exchange powershell output, how to count powershell, get count for powershell output
June 7th, 2011 at 4:24 pm
Good one Ratish. Thank you.
August 16th, 2011 at 4:44 am
A method that I normally use is to run the commands like this:
(Get-ExchangeServer).Count
or
(Get-Mailbox -ResultSize Unlimited).Count
for example, which will just return an integer with the number of objects found. I find this useful in scripts!
December 25th, 2011 at 1:24 pm
how to email this mailbox count from server to my mailbox through script.
December 28th, 2011 at 5:32 pm
You can do something like this (not tested!):
Send-MailMessage -From “admin@domain.com” -To “user@domain.com” -Subject “Mailbox Count” -Body $((Get-Mailbox -ResultSize Unlimited).Count) -SMTPserver “smtp.domain.com” -DeliveryNotificationOption onFailure
March 12th, 2012 at 3:17 pm
Thank you. In my case I wanted to be able to get it down to just an integer with Select-Object (found after some googling) so I could subtract it from my total mailbox count. First system mailboxes then disconnected mailboxes:
Get-MailboxStatistics -Database dbname | where {$_.DisplayName -match “System”} | Measure-Object | Select-Object -expand Count
Get-MailboxStatistics -Database dbname | where {$_.DisconnectDate -ne $null} | Measure-Object | Select-Object -expand Count