Check if Object has Property in PowerShell

Check if Object contains property in PowerShell

Using the -match Parameter

Use the -match parameter to check if an object has a property in PowerShell.

Here, we used the -match parameter to check if the given object, which is $result in our case, has the greetings property. Now, you can refer to this article to learn how the $result object is created.

The -match parameter will match and return the property name if it exists and nothing if the specified property does not exist. See the following examples to understand.

See, we only got the greetings property name which already exists and did not get any error or warning for the second command saying that first_name does not exist. This is why we used [bool] to explicitly cast it to Boolean to get True if the property matched; otherwise, False.

This process of explicitly casting data type is called type casting. For example, if we do not want to use [bool] but want to get results in Boolean (True/False), then the following solution will work for us.

Using -contains Parameter

Use the -contains parameter to check if an object has a property in PowerShell.

We used the -contains parameter to get Boolean output. So, for instance, we will get True if the specified object contains the given property; otherwise, False.

Using if-else Block

Use the if-else block to check if an object has a property in PowerShell.

In the above code, we used the if-else block to check if the given object has the specified property. Here, we used dot notation to target a specific property of the object. Next, we used echo to print True if the given condition was satisfied; otherwise, False.

Using .Match() Function with if-else Block

Use the .Match() function to check if an object has a property in PowerShell.

This code is similar to the previous example, where we only used the if-else block to check if an object has a particular property. Here, we used the .Match() function within the if condition, which took the property name greetings as an argument. Again, like the previous example, we printed True if the if condition is fulfilled; otherwise, False.

Using Get-Member Cmdlet

Use the Get-Member cmdlet to get the property-related details if it exists in the specified object.

We used the Get-Member cmdlet because we were interested in checking if the given property exists and wanted to know the property name, its value and MemberType.

The Get-Member cmdlet is used to get the given objects’ members, methods and properties. We used the -InputObject parameter to specify or pipe the object to Get-Member.

We used the -MemberType parameter to get specific types of members, for instance, NoteProperties; otherwise, we can use the -Static parameter to get information about static members.

Note that the Get-Member cmdlet returns a list containing alphabetically ordered members where methods are listed first. We can use the Get-Member cmdlet with the if-else block.

The above code resulted in True if the specified condition is fulfilled; otherwise, False. We can also use the Get-Member cmdlet’s alias as follows.

That’s all about check if object has property in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *