I assume you’re using Set-StrictMode, if you’re getting errors when you try to access a non-existent property. By default, PowerShell just returns $null when you do that.
To test for the existence of a property, you can use the PSObject.Properties table:
$testObject = New-Object psobject -Property @{ SomeProperty = 'SomeValue' } if ($testObject.PSObject.Properties['SomeProperty']) { Write-Host "SomeProperty: $($testObject.SomeProperty)" } if ($testObject.PSObject.Properties['BogusProperty']) { Write-Host "BogusProperty: $($testObject.BogusProperty)" }
Edit: Get-Member works too, as Don mentioned:
if (Get-Member -InputObject $testObject -Name SomeProperty -MemberType Properties) { Write-Host "SomeProperty: $($testObject.SomeProperty)" }