PowerShell: $null and parameter type
One of those things that can take you ages to find out: I create a function where a parameters that can either have a string value or can be $null:
function doit
{
param
(
[string]$a = $( throw "Missing: parameter a"),
$b = $( throw "Missing: parameter b")
)
if ($a -eq $null) { Write-Host "a is null" } else { write-Host "a is not null" }
if ($b -eq $null) { Write-Host "b is null" } else { Write-Host "b is not null" }
}
If I call this function with: doit $null $null
I get the following result:
a is not null
b is null
What happened: because I specified a type [string] for parameter a, the $null value gets casted to a string with value "".