146

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

Appreciate I can do ...

$members = Get-Member -InputObject $myobject 

and then foreach through the $members, but is there a function to test if the object has a specific property?

Additional Info: The issue is I'm importing two different sorts of CSV file, one with two columns, the other with three. I couldn't get the check to work with "Property", only with "NoteProperty" ... whatever the difference is

if ( ($member.MemberType -eq "NoteProperty" ) -and ($member.Name -eq $propertyName) ) 
5
  • 13
    ($object.property -ne $null) ?
    – arco444
    Commented Nov 18, 2014 at 15:12
  • 5
    Does it matter if the propery does exist but has a $null value
    – Matt
    Commented Nov 18, 2014 at 16:11
  • 2
    @arco444 nope -- that requires the property to exist for it to succeed. -- If you have dynamic data (i.e. from a web request) where some rows don't even specify that property, that check will fail. :( Commented May 25, 2018 at 20:15
  • The upvotes (at the time of writing this) do not reflect the best answers (and actually suggest that the slowest answer is better than the fastest answers).
    – Dave F
    Commented Dec 27, 2021 at 15:25
  • @Matt solutions that check if the property exists using PSObject.Properties will return a value that evaluates to $True even when the property has a value that evaluates to false, such as $Null, $False, 0, or the empty string. This is because PSObject.Properties is a collection of objects with information about the object's properties (and objects evaluate to true).
    – Dave F
    Commented Dec 27, 2021 at 15:35

18 Answers 18

141

Like this?

 [bool]($myObject.PSobject.Properties.name -match "myPropertyNameToTest")
10
  • 17
    This answer works only for powerShell v3 or higher. The following should work even with powerShell v2 and above: [bool]($file.psobject.Properties | where { $_.Name -eq "myPropertyNameToTest"})
    – Patrick
    Commented Sep 21, 2015 at 12:03
  • 56
    $myObject.PSobject.Properties.Name -contains "myPropertyNameToTest" is better. No need to worry about possible pattern matching, and you won't need to cast to Boolean then, either.
    – Bacon Bits
    Commented Dec 15, 2016 at 19:31
  • 5
    What if the object has the property ThisisMyProperty and you'd like to check for MyProperty? All solutions in this answer will result in a false positive. Commented Sep 13, 2017 at 10:23
  • 5
    @KolobCanyon That will not function under Strict Mode. Commented Feb 5, 2018 at 20:46
  • 5
    This code will give a false positive if for example you where testing for "Count" and the object had a property called "ThingCount".
    – dan-gph
    Commented Feb 26, 2018 at 6:38
110

You can use Get-Member

if (Get-Member -inputobject $var -name "Property" -Membertype Properties) {
    #Property exists
}
4
  • 17
    Best answer IMHO. This works with .NET objects that don't have a PSobject member. We can also remove the -Membertype option if we don't care which type of member the object contains - just the member name. Very useful with Invoke-RestMethod or ConvertFrom-Json objects!
    – Mister_Tom
    Commented Apr 14, 2017 at 20:59
  • 14
    Short syntax without checking type: if($var | Get-Member Property){ }, even shorter but less readable: if($var | gm Property){ }
    – zett42
    Commented Dec 17, 2019 at 13:48
  • 1
    This is in my opinion the best answer. Commented Aug 24, 2020 at 6:36
  • This works with strict mode 3 too, while the other alternatives fail in certain (probably corner) cases.
    – Yennefer
    Commented Dec 30, 2023 at 16:35
35

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
}
2
  • 1
    I love this answer, but it seems to not work in some cases where CB's answer does. Commented Mar 31, 2019 at 7:12
  • 1
    Or, where "succinct" = "needless junk introduced via Strict Mode without a good fallback usage case" (ie. no ?. equivalent) :| This will also fail for certain variations of $MyObject, notably $null. Such was not the case with the null-chaining outside of Strict Mode. Commented Sep 6, 2019 at 21:25
17

There are a number of solutions to this question that work in strict mode, but some are better than others.

Solutions that do not appear to iterate through every property are the fastest solutions.

  1. Bernie White's solution and
  2. esskar's solution (modified)

Solutions that look as though they iterate through every property are slower.

  1. sebke CCU's solution and
  2. dan-gph's solution

The solution that appears to iterate through every property and uses a regular expression is a little slower than the previous two solutions (because compiling and executing the regular expression takes more time)

  1. CB.'s solution

The solution that uses GetMethod appears to iterate through every property, but its use of GetMethod makes it significantly slower.

  1. Paul's GetMethod solution

The following script was used to compare the previously mentioned solutions in strict mode:

# Tested in PowerShell core 7.2.0

Set-StrictMode -Version Latest

$propertyExistsMethods = New-Object System.Collections.Generic.Dictionary'[string,scriptblock]'

# Fastest
$propertyExistsMethods.Add(
 "PSObject.Properties (Bernie White's solution)",
{
  Param( [PSObject] $Object, [string] $Property )
  [bool]$Object.PSObject.Properties[$Property]
})
$propertyExistsMethods.Add(
 "PSObject.Properties.Item (esskar's solution (modified))",
{
  Param( [PSObject] $Object, [string] $Property )
  [bool]$Object.PSObject.Properties.Item($property)
})

# Not as fast
$propertyExistsMethods.Add(
 "Contains (sebke CCU's solution)",
{
  Param( [PSObject] $Object, [string] $Property )
  $Object.PSobject.Properties.Name.Contains($Property)
})
$propertyExistsMethods.Add(
 "-in (dan-gph's solution)",
{
  Param( [PSObject] $Object, [string] $Property )
  $Property -in $Object.PSobject.Properties.Name
})

# Slower than the previously mentioned solutions
$propertyExistsMethods.Add(
 "-match (CB.'s solution)",
{
  Param( [PSObject] $Object, [string] $Property )
  [bool]($Object.PSobject.Properties.name -match $Property)
})

# Slowest
$propertyExistsMethods.Add(
 "GetMember (Paul's solution)",
{
  Param( [PSObject] $Object, [string] $Property )
  Get-Member -inputobject $Object -name $Property -Membertype Properties
})

foreach ($method in $propertyExistsMethods.Keys) {
  $propertyExists = $propertyExistsMethods[$method]

  $o = @{}
  foreach ($i in 1..100000) {
    $o[$i] = "p$i"
  }

  Write-Host $method
  $measure = Measure-Command {
    foreach ($i in 1..100000) {
      # Always check for a property that does NOT exist
      & $propertyExists -Object $o -Property 'p'
    }
  }
  Write-Host $measure | % { $_.Milliseconds }
  Write-Host ''
}

The output is as follows:

PSObject.Properties (Bernie White's solution)
00:00:03.1437587

PSObject.Properties.Item (esskar's solution)
00:00:03.5833642

Contains (sebke CCU's solution)
00:00:04.4812702

-in (dan-gph's solution)
00:00:04.6507811

-match (CB.'s solution)
00:00:05.1107066

GetMember (Paul's solution)
00:00:14.5305115
13

For me MyProperty" -in $MyObject.PSobject.Properties.Name didn't work, however

$MyObject.PSobject.Properties.Name.Contains("MyProperty")

works

2
  • Does not work for hash tables, which do not have a Name property.
    – Diti
    Commented Jun 21, 2022 at 11:40
  • 2
    For hashtables you can just use ContainsKey() @Diti
    – Jon R
    Commented Nov 2, 2022 at 22:09
11

Try this for a one liner that is strict safe.

[bool]$myobject.PSObject.Properties[$propertyName]

For example:

Set-StrictMode -Version latest;
$propertyName = 'Property1';
$myobject = [PSCustomObject]@{ Property0 = 'Value0' };

if ([bool]$myobject.PSObject.Properties[$propertyName]) {
    $value = $myobject.$propertyName;
}
2
7

Just check against null.

($myObject.MyProperty -ne $null)

If you have not set PowerShell to StrictMode, this works even if the property does not exist:

$obj = New-Object PSObject;                                                   
Add-Member -InputObject $obj -MemberType NoteProperty -Name Foo -Value "Bar";
$obj.Foo; # Bar                                                                  
($obj.MyProperty -ne $null);  # False, no exception
4
  • 3
    As far as I can see, this works if the property exists and is $null, but not if the property does not exists - attempting to access it to do the null check throws an exception.
    – Peter
    Commented Feb 27, 2017 at 8:39
  • @Peter Can you provide an example of the null check throwing an exception when the property does not exist. I have added an example in which a property does not exist and there is no exception. Commented Feb 27, 2017 at 17:08
  • 3
    Run this: Set-Strictmode -version Latest;$obj = ConvertFrom-Json -InputObject '{"name":"test", "version":"1.1.0"}';If($obj.StartDate -ne $null){Write-Verbose -Message $obj.startDate -Verbose} and you will get an error Property 'StartDate' cannot be found on this object. However I need to qualify my comment - you don't get it if Strictmode is not set. I always have it set, so never realised until I tested for this! Still, I think most people use (or should use) 'Set-Strictmode'.
    – Peter
    Commented Feb 28, 2017 at 8:01
  • 2
    Probably best if you qualify your answer, and I'll remove my downvote? Everyone has learnt something, which is the point :-)
    – Peter
    Commented Feb 28, 2017 at 8:07
5

I've been using the following which returns the property value, as it would be accessed via $thing.$prop, if the "property" would be to exist and not throw a random exception. If the property "doesn't exist" (or has a null value) then $null is returned: this approach functions in/is useful for strict mode, because, well, Gonna Catch 'em All.

I find this approach useful because it allows PS Custom Objects, normal .NET objects, PS HashTables, and .NET collections like Dictionary to be treated as "duck-typed equivalent", which I find is a fairly good fit for PowerShell.

Of course, this does not meet the strict definition of "has a property".. which this question may be explicitly limited to. If accepting the larger definition of "property" assumed here, the method can be trivially modified to return a boolean.

Function Get-PropOrNull {
    param($thing, [string]$prop)
    Try {
        $thing.$prop
    } Catch {
    }
}

Examples:

Get-PropOrNull (Get-Date) "Date"                   # => Monday, February 05, 2018 12:00:00 AM
Get-PropOrNull (Get-Date) "flub"                   # => $null
Get-PropOrNull (@{x="HashTable"}) "x"              # => "HashTable"
Get-PropOrNull ([PSCustomObject]@{x="Custom"}) "x" # => "Custom"
$oldDict = New-Object "System.Collections.HashTable"
$oldDict["x"] = "OldDict"
Get-PropOrNull $d "x"                              # => "OldDict"

And, this behavior might not [always] be desired.. ie. it's not possible to distinguish between x.Count and x["Count"].

1
  • 1
    To get a property or $null without extra function: $x = try{ $someObject.someProp } catch{ $null }. You can omit $null from the catch block, but IMO it improves readability.
    – zett42
    Commented Nov 17, 2020 at 14:15
3

If you are using StrictMode and the psobject might be empty, it will give you an error.

For all purposes this will do:

    if (($json.PSobject.Properties | Foreach {$_.Name}) -contains $variable)
3

I find this method more strict and faster when checking multiple properties

$null -ne $myobject.PSObject.Properties.Item("myPropertyNameToTest")
1
  • 3
    You don't need to compare the property from PSObject.Properties to $null. It can be cast to a boolean value like so [bool]$myobject.PSObject.Properties.Item("myPropertyNameToTest")
    – Dave F
    Commented Dec 27, 2021 at 15:00
2

Real similar to a javascript check:

foreach($member in $members)
{
    if($member.PropertyName)
    {
        Write $member.PropertyName
    }
    else
    {
        Write "Nope!"
    }
}
5
  • 1
    This may not work if the property actually has the value $null. Commented Jan 21, 2017 at 2:09
  • 8
    This will fail if PowerShell is in strict mode.
    – Ian Kemp
    Commented May 11, 2017 at 12:24
  • @IanKemp What is strict mode? Like the Execution Policy? Commented Oct 5, 2017 at 15:22
  • 1
    @KolobCanyon learn.microsoft.com/en-za/powershell/module/… - essentially it's the PS equivalent to JavaScript's use strict.
    – Ian Kemp
    Commented Oct 6, 2017 at 5:46
  • 1
    This will also not work if the property exists, and is set to $false.
    – user2883951
    Commented Mar 6, 2019 at 18:14
1

Just to clarify given the following object

$Object

With the following properties

type        : message
user        : [email protected]
text        : 
ts          : 11/21/2016 8:59:30 PM

The following are true

$Object.text -eq $NULL
$Object.NotPresent -eq $NULL

-not $Object.text
-not $Object.NotPresent

So the earlier answers that explicitly check for the property by name is the most correct way to verify that that property is not present.

0

I ended up with the following function ...

function HasNoteProperty(
    [object]$testObject,
    [string]$propertyName
)
{
    $members = Get-Member -InputObject $testObject 
    if ($members -ne $null -and $members.count -gt 0) 
    { 
        foreach($member in $members) 
        { 
            if ( ($member.MemberType -eq "NoteProperty" )  -and `
                 ($member.Name       -eq $propertyName) ) 
            { 
                return $true 
            } 
        } 
        return $false 
    } 
    else 
    { 
        return $false; 
    }
}
0

I recently switch to set strict-mode -version 2.0 and my null tests failed.

I added a function:

#use in strict mode to validate property exists before using
function exists {
  param($obj,$prop)
  try {
    if ($null -ne $obj[$prop]) {return $true}
    return $false
  } catch {
    return $false
  }
  return $false
}

Now I code

if (exists $run main) { ...

rather than

if ($run.main -ne $null) { ...

and we are on our way. Seems to work on objects and hashtables

As an unintended benefit it is less typing.

1
  • For null or empty, I've always used: IF([string]::IsNullOrEmpty($userID)) { write-host "Null or empty"}
    – user2883951
    Commented Mar 6, 2019 at 18:27
0

for me this work

Set-StrictMode -Version Latest
$TMP = ...

$HAS_SERVERS=($TMP | Select-Object Servers)
if (-not $HAS_SERVERS.Servers){
    echo "No servers. Abort."
} else {
    ...
}
 
-1

I just started using PowerShell with PowerShell Core 6.0 (beta) and following simply works:

if ($members.NoteProperty) {
   # NoteProperty exist
}

or

if (-not $members.NoteProperty) {
   # NoteProperty does not exist
}
1
-1

You could check with:

($Member.PropertyNames -contains "Name") this will check for the Named property

-1

For identifying which of the objects in an array have a property

$HasProperty = $ArrayOfObjects | Where-Object {$_.MyProperty}
1
  • 1
    Fails in StrictMode when the property does not exist. Commented Sep 11, 2019 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.