Powershell - $false -ne 'false' - Boolean values

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Hello,

Demonstration and explanation $false -ne ‘false’, plus a point about Boolean values.

# Let's display the values of the variables $True and $False.
$False
$True
# We check the value type of these 2 variables
$False.GetType()
$True.GetType()
# Let's check a few behaviours (try to guess the result for each)
$false -ne 'false
false' -ne $false
'' -ne $false
$false -ne ''
0 -ne $false
$false -ne 0
# Explanations
# A string value can be evaluated as Boolean
# An empty string like '' corresponds to $False
$false -ne ''
# A non-empty string such as 'False' matches $True
$false -ne 'false
# When using -eq or -ne the value on the right is converted to the type of value on the left.
# Values of the same type are evaluated
# false' is a string value and [string]$false is 'false
false' -ne $false
'' -ne $false
# An integer value can be evaluated as a Boolean
# 0 corresponds to $False
# A number other than 0 corresponds to $True
0 -ne $false
$false -ne 0
# Test with $True and equivalent
# Try to guess the result
$true -eq 'true
true' -eq $true
'' -eq $true
$true -eq ''
1 -eq $true
$true -eq 1
# Other values evaluated as Booleans
# A non-empty array is $True
$false -eq @(1,2,3,4,5)
# An empty array is $False
$false -eq @()
# A Powershell command that executes correctly is worth $True
$false -eq (Get-Service)
# A Powershell command which does not execute correctly is worth $False
$false -eq (Get-Service -name altf4formation -ErrorAction SilentlyContinue)
# An undefined, empty or null variable is worth $False
# Small code change related to the behaviour of -eq with $null.
if ($a) { 'TRUE' } else { 'FALSE' }
if ($Null) { 'TRUE' } else { 'FALSE' }
# -eq and -ne do not perform object type consistency with $Null
$false -eq $null
$true -eq $null
'' -eq $Null
$Null -eq ''
# Only a comparison between 2 $Null values returns $True
$Null -eq $Null
$b -eq $Null

Related links