PowerShell Conditional logic If conditional shorthand


If you want to use the shorthand you can make use of conditional logic with the following shorthand. Only the string 'false' will evaluate to true (2.0).

#Done in Powershell 2.0 
$boolean = $false;
$string = "false";
$emptyString = "";

    # this does not run because $boolean is false
    Write-Host "Shorthand If conditions can be nice, just make sure they are always boolean."

    # This does run because the string is non-zero length
    Write-Host "If the variable is not strictly null or Boolean false, it will evaluate to true as it is an object or string with length greater than 0."

    # This does not run because the string is zero-length
    Write-Host "Checking empty strings can be useful as well."

    # This does not run because the condition is null
    Write-Host "Checking Nulls will not print this statement."