Bob Bob - 1 year ago 122
PowerShell Question

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

In Powershell, what does

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

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download