PowerShell comparison

Hello everybody,

today I want to share another trick from PowerShell activities. 

So, imagine, that you see following PowerShell code:

"048" -eq 48

48 -eq "048"

What do you expect to see? True, True or True, False ....

Here is the output:

PS C:\sources> "048" -eq 48

48 -eq "048"

False

True

PowerShell has interesting behaviour in this case. It takes value on the right, and converts it to the value on the left. 

For the first case PS will make following comparison:

convert 48 to string, which will be "48". Compares "48" with "048" and produce False.

No Comments

Add a Comment