A while ago I found a blog post explaining how to shortcut testing NULL values with PowerShell.
Do not do that!
I agree with the quote on the blog:
One thing you may not forget is that Powershell is a lot more friendly for NULL values than C#, so don’t forget to check your objects for NULL values. In Powershell this is very clean and easy to do.
But it is also easy to get wrong:
To see if a variable is null, simply check:
If (!$Variable) {some action}Conversely, to verify if the variable has any value:
If ($Variable) {some action}
Just a few examples. Now quess the outcome for all of them.
$a=$null; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=$false; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=0; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=''; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=""; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
$a=1; if ($a) {"'$a' has VALUE"} else {"'$a' is NULL"}
Now take an educated guess on the outcome. Read the rest of this entry »


