Why does the expression "1" -eq 1
comes TRUE in powershell ? The first one is of string type and the second one is of integer type why is it treating them as equal.
-
PowerShell tries to be helpful by converting the LHS operand to the RHS operand type, before executing any expression. – Abraham Zinala Mar 20 '22 at 04:26
-
don't spam tags. Version-specific tags are only for issues that occur in that specific version – phuclv Mar 20 '22 at 04:31
-
@phuclv i am sorry I am new here I will keep that in mind from now on. – user60845 Mar 20 '22 at 04:33
-
@AbrahamZinala thankyou sir for answering the question it helped me a lot. – user60845 Mar 20 '22 at 04:33
1 Answers
This is called implicit type conversion.
Powershell automatically perform a type conversion when a value of one type is used in a context that requires a different type.
A type conversion is performed when a value of one type is used in a context that requires a different type. If such a conversion happens automatically it is known as implicit conversion. (A common example of this is with some operators that need to convert one or more of the values designated by their operands.) Implicit conversion is permitted provided the sense of the source value is preserved, such as no loss of precision of a number when it is converted.
In addition to the official documentation, here is an excellent answer by mklement0 here with some additional examples and references: PowerShell Implicit Type Conversions - does it happen? Is it safe?

- 7,292
- 2
- 22
- 26

- 9,932
- 3
- 27
- 39