There is this weird thing in Powershell that you might have come across if you have ever counted how many objects you have. In some cases Powershell will count 0 $null 2 3…
You could say that this is a side effect of how Powershell tries to "magically" simplify stuff. The thing is, it doesn't always happen, instead it depends on what it is you want to count. As an example it will happen when counting ADUsers but it won't happen when counting processes. In the example below there is a user called Patrik yet the count returns nothing, well, in reality it returns $null.
PS C:\> (Get-ADUser patrik -Properties DisplayName).DisplayName Patrik Johansson PS C:\> (Get-ADUser -Filter { DisplayName -like "*Patrik*" }).count PS C:\> (Get-ADUser -Filter { DisplayName -like "*Karl*" }).count 0 PS C:\> (Get-ADUser -Filter { DisplayName -like "*Anders*" }).count 2 PS C:\>
This could lead to some very unfortunate results if it where to happen in your script. Fortunately there is a very easy fix for it. Simply adding a @ before the parentheses forces Powershell to see it as an array and then it will count it just fine.
PS C:\> @(Get-ADUser -Filter { DisplayName -like "*Patrik*" }).count 1 PS C:\>
This is one of those weird things that after a while will come natural for you but that you will miss over and over in the beginning.
Happy scripting!