PowerShell Logic

Seeing the help topic about_Comparison_Operators of PowerShell I understand this:

PS C:\> $false,$false -eq $true PS C:\> 

Nothing on the left fits right, so nothing is returned even $null .

I do not understand this:

 PS C:\> $true -eq $false,$false True PS C:\> 

This is because it first executes $true -eq $false , which returns False , and then accepts this False and makes $false -eq $false , which returns True ?

Additional Information

The reason why below returns false is because it compares a string with an array, right? The string is not equal to the array.

 PS C:\> "abc" -eq "abc","def" False 

Answer

More digging shows that $true is equal to the object.

 PS C:\> $true -eq [array] True PS C:\> $true -eq [string] True PS C:\> $true -eq [int] True PS C:\> $true -eq [bool] True 

The values โ€‹โ€‹of these objects matter.

 PS C:\> $true -eq [int]0 False 
+8
equals logic powershell compare
source share
3 answers

I like fundamental questions about the behavior and features of such a language ... Give me a reason to read the PowerShell language specification.

You can download the specified specification: 2.0 and 3.0 . See Section 7.8.1 - Equality and Relational Operators.

In the first part of the question, something is actually returned - an empty array, which is illustrated: ($false,$false -eq $true).psbase

 Length : 0 LongLength : 0 Rank : 1 SyncRoot : {} IsReadOnly : False IsFixedSize : True IsSynchronized : False Count : 0 

From spec -

If the value indicated by the left operand is not a collection, the result is of type bool. Otherwise, the result may be an empty unlimited 1-dimensional array containing elements that check True in comparison with the value indicated by the right operand.

For the second part, because the left operand itself is a bool, I think it will always be the result. This only happens when the right operand is a collection.

Some examples:

 $true -eq 1,2,3,4 $true -eq '','','','' $true -eq '',1,$true,$false $true -eq $null,$false,1,'' 

They all return $true . Conversely, all of them return $false

 $false -eq 1,2,3,4 $false -eq '','','','' $false -eq '',1,$true,$false $false -eq $null,$false,1,'' $false -eq $true,$true,$true 

The type of left operand is very important. This wil returns $true : $false -eq 0 , because the right operand can be applied to the type of the left operand.

+7
source share

Best guess:

It evaluates to True because it is an array with several members. The array does not have a ToBoolean () method.

As long as it is a scalar or array that has only one member, Powershell can force it to a scalar and go to the ToBoolean () method in the member. As soon as it goes to 2 or more, it always returns True, regardless of what the elements are. If there is no ToBoolean (), and it returns to the standard ToString () method, which will always evaluate to True.

Or maybe not.

+3
source share

Perhaps the -eq operator evaluates their existence (aka not $null ), and not their value.

Perhaps, as is the case with IF?

 PS> if("anyval"){$true}else{$false} True 

This answer provided by @Joey may also be helpful.

+2
source share

All Articles