SoFunction
Updated on 2025-03-10

PowerShell tips: True and False type conversion

When judging conditions, $True and $False are indispensable. When converting other types into Bool types, there are several points to pay attention to:

Convert other types to boolean types

PS> 0,1,-1,'0','1','true','false',$null | foreach { [bool]$_ }
False
True
True
True
True
True
True
False

Summary: Only integers 0 and Null can be converted to False, and others will be cast to True

Boolean type converted to string

Copy the codeThe code is as follows:
PS> $true,$false | foreach { $_.ToString() } True False

There should be no suspense about this.

Boolean type converted to integer

Copy the codeThe code is as follows:
PS> $true,$false | foreach { [int] $_ } 1 0

It is also understandable that 1 and 0 represent true and false, respectively.