How can you test if an object has a specific property? How can you test if an object has a specific property? powershell powershell

How can you test if an object has a specific property?


Like this?

 [bool]($myObject.PSobject.Properties.name -match "myPropertyNameToTest")


You can use Get-Member

if(Get-Member -inputobject $var -name "Property" -Membertype Properties){#Property exists}


This is succinct and readable:

"MyProperty" -in $MyObject.PSobject.Properties.Name

We can put it in a function:

function HasProperty($object, $propertyName){    $propertyName -in $object.PSobject.Properties.Name}