Table of Contents
Using the -match
Parameter
Use the -match
parameter to check if an object has a property in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$greetings = "Hi" $name = "Java2blog" $props = $([ordered]@{ greetings=$greetings name=$name }) $results = New-Object psobject -Property $props [bool] ($results.PSobject.Properties.name -match "greetings") |
1 2 3 |
True |
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.
1 2 3 4 |
$results.psobject.Properties.name -match "greetings" $results.psobject.Properties.name -match "first_name" |
1 2 3 |
greetings |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
$greetings = "Hi" $name = "Java2blog" $props = $([ordered]@{ greetings=$greetings name=$name }) $results = New-Object psobject -Property $props $results.PSobject.Properties.name -contains "greetings" |
1 2 3 |
True |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$greetings = "Hi" $name = "Java2blog" $props = $([ordered]@{ greetings=$greetings name=$name }) $results = New-Object psobject -Property $props if($results.greetings){ echo "True" }else{ echo "False" } |
1 2 3 |
True |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$greetings = "Hi" $name = "Java2blog" $props = $([ordered]@{ greetings=$greetings name=$name }) $results = New-Object psobject -Property $props if($results.PSObject.Properties.Match('greetings')){"True"} else{"False"} |
1 2 3 |
True |
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
.
Further reading:
Using Get-Member
Cmdlet
Use the Get-Member
cmdlet to get the property-related details if it exists in the specified object.
1 2 3 4 5 6 7 8 9 10 11 12 |
$greetings = "Hi" $name = "Java2blog" $props = $([ordered]@{ greetings=$greetings name=$name }) $results = New-Object psobject -Property $props Get-Member -inputobject $results -name "greetings" -Membertype Properties |
1 2 3 4 5 6 7 |
TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- greetings NoteProperty string greetings=Hi |
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.
1 2 3 4 5 6 |
if(Get-Member -inputobject $results -name "greetings" -Membertype Properties){ "True" } else{"False"} |
1 2 3 |
True |
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.
1 2 3 4 |
if(gm -inputobject $results -name "greetings" -Membertype Properties){"True"} else{"False"} |
1 2 3 |
True |
That’s all about check if object has property in PowerShell.