PowerShell, finding data in an object returned from a cmdlet

I am new to PowerShell and am trying to understand how to search the results that get returned from a cmdlet.  

I realize this example could be accomplished another way, but I'm just playing around, so please forgive me if the actual use case is a little vague.

In this case, I'm trying to determine if the current computer is a server or not.  I assign a variable ($objListOfNonServers) to receive the collection of objects that get returned from the cmdlet (Get-ADComputer) which are not running Server OSes and which have the same machine name as the current machine.  Essentially, this should contain one object if the current machine is not a server in AD, or no objects if it is running a server OS.  But then I don't know how to test whether that collection contains any objects or not.  I would have thought the $objListOfServers.COUNT method would equal 0 if there were no matches, but I'm doing something wrong.  Can you help me figure out how to determine if the current machine is running a server operating system or not?

Here is my code

# Get this computer name
$sComputerName = $env:computername
#get list of computers that are not servers and have the name of this machine
$objListOfNonServers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Windows Server*' -and Name -Like $sComputerName } | Select Name
if ($objListOfNonServers.Count -eq 0)
    { Write-Output 'This machine is a desktop' }
    { Write-Output 'This machine is NOT a desktop' }
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can apply methods or ask for properties only on objects containing something. If you get nothing, the object var contains $null, and not really an object. There are two ways to check:
1. You make it an array by enclosing the expression or var into @(...), and then check .Count - an array always has that property.
2. You just check against $null.
if (@($objListOfNonServers).Count) # ...
# or:
if ($objListOfNonServers) # ...

Open in new window

As you can see, I do not really use any comparison operator here - I make use of the fact that $null is considered $false, and the same applies for numeric 0. Anything different from a numeric 0 is $true.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
malcolm29Author Commented:
Concise, correct, and examples given
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.