Powershell treats empty string as equivalent to null in switch statements but not if statements Powershell treats empty string as equivalent to null in switch statements but not if statements powershell powershell

Powershell treats empty string as equivalent to null in switch statements but not if statements


Powershell auto-magically casts a $null to an empty string. Consequently, when using $null on a .NET API call, Powershell actually casts it to an empty string. To pass an actual null value in an API call, use [NullString]::Value instead.


I think is a bug of powershell 2.0 (here some info on MSFT Connect).

I can say that in v 3.0 you code return out here!


The following statements show that $null is not equivalent to the empty string in a switch statement.

$a=""; switch ($a){$null {"null"} "" {"empty string"}}$a=$null; switch ($a){$null {"null"} "" {"empty string"}}