Hi Edmond,
I am not incorrect.
See Example Below:
Remove-Variable "upws"
If ($upws) {'$upws is True'}Else{'$upws is not True'}
If ($upws -eq $False) {'$upws is True'}Else{'$upws is not True'}
Results:
$upws is not True
$upws is not True
In the above example, in the first instance $upws does not exist so the result is not true. In the second instance $upws still does not exist, but it’s value is not false either so the result is still not true.
Let’s give a second example:
$upws = $false
If ($upws) {'$upws is True'}Else{'$upws is not True'}
If ($upws -eq $True) {'$upws is True'}Else{'$upws is not True'}
If ($upws -eq $False) {'$upws is True'}Else{'$upws is not True'}
Results:
$upws is not True
$upws is not True
$upws is True
In the above example, $upws now exists, but it’s value is set to false. If the “If ($upws)” statement condition only checked to see if the variable existed, then the results would be “$upws is True”; however we see the result is “$upws is not True”. This is because the if statement is not only checking that the variable exists, but also checking it’s value. When we additionally check the values for $True/$False, we see the results are expected for the value.
Now let’s bring it back to the example in this thread:
$upws = [pscustomobject]@{property = $false}
If ($upws) {'$upws is True'}Else{'$upws is not True'}
If ($upws -eq $True) {'$upws is True'}Else{'$upws is not True'}
If ($upws -eq $False) {'$upws is True'}Else{'$upws is not True'}
Results:
$upws is True
$upws is not True
$upws is not True
As we see from the above result, even though the property value is $false, the “If ($upws)” statement condition resolves true. This is because there is an object in the variable, it exists and has value. The “If ($upws -eq $True)” statement condition resolves not true because even though it exists and has value, that value is not equal to a boolean $True. Same thing with “If ($upws -eq $False)”. That’s because both of these conditions are looking at the value of $upws, which is an object, not’s $false, which is the value of the “property” property on the object.
Last Example:
$upws = [pscustomobject]@{property = $false}
If ($upws.property) {'$upws is True'}Else{'$upws is not True'}
If ($upws.property -eq $True) {'$upws is True'}Else{'$upws is not True'}
If ($upws.property -eq $False) {'$upws is True'}Else{'$upws is not True'}
Results:
$upws is not True
$upws is not True
$upws is True
Again we see that the “If ($upws.property)” statement condition resolves not true, event though we know the property exists on the object. This is because it is also testing the value of the property, not just whether or not it exists. “If ($upws.property -eq $True)” resolves not True because the value of “property”, Boolean $False, it not equal to Boolean $True. And “If ($upws.property -eq $False)” resolves true because the value of “property”, Boolean $False, does equal Boolean $False.
Hope that helps.