Bob Bob - 4 months ago 29
PowerShell Question

Powershell - What does if($variable) test for?

In Powershell, what does

if($variable)
test for? Is this testing if the variable is set, is null, is true, or something else?

Answer

It tests whether the variable is true or whether a non-Boolean variable can be coalesced to true. For example, each of these return false:

$var #uninitialized
$var = ""
$var = $false
$var = 0

Each of these return true:

$var = "something"
$var = $true
$var = 1 #or any non-zero number