This is a quick PowerShell tip. Are you used to working with really large number for enterprise work? Yeah, me too. I pretty much always need “-resultsize unlimited” whenever I write a command. Sometimes, however, you can run into just one problem – the problem of “one”.
Here’s a sample of how to create the problem. Make a script that looks like this:
(Get-Mailbox -Filter {WindowsEmailAddress -like'*contoso.com'}).Count
42
Okay. So this is working, right? Yeah, not so fast. What happens with a really small set, like “one”?
(Get-Mailbox -Filter {WindowsEmailAddress -eq'postmaster@contoso.com'}).Count
We get no results! Now, you know that email address exists, of course. Trying it without the “count” works fine. So what’s up? Well, the “.Count” function returns the count of an array. If you only have one object, it’s not an array, so you can’t count just the one object. Just the thing to drive you crazy for an afternoon.
So, what’s a more reliable way to count objects when you are doing a report, or something where you could possibly run into the “one” problem? Try this:
(Get-Mailbox -Filter {WindowsEmailAddress -eq'postmaster@contoso.com'} | Measure-Object).Count
1
Ah! Just the “one” I was looking for! The master of Powershell, Ed Wilson, has a great post showing all about counting objects.