Optional String Parameter (should be NULL)
<p>I try to introduce an optional string parameter to my function. <a href="https://stackoverflow.com/questions/22906520/powershell-string-default-parameter-value-does-not-work-as-expected">Based on this thread should</a> <code>[AllowNull()]</code> do the trick, but PowerShell still populates my parameter with an empty string (using PowerShell version 5.1.14393.206).</p>

<p>The following function illustrates the problem:</p>

<pre><code>function Test-HowToManageOptionsStringParameters() {
    Param(
        [Parameter(Mandatory)]
        [int] $MandatoryParameter,
        [Parameter()]
        [AllowNull()]
        [string] $OptionalStringParameter = $null
    )

    if ($null -eq $OptionalStringParameter) {
        Write-Host -ForegroundColor Green 'This works as expected';
    } else {
        Write-Host -ForegroundColor Red 'Damit - Parameter should be NULL';
    }
}
</code></pre>

<p>To makes think even worse is even this code not working (assigning <code>$null</code> to the parameter for testing), I really do not understand why this is not working…</p>

<pre><code>function Test-HowToManageOptionsStringParameters() {
    Param(
        [Parameter(Mandatory)]
        [int] $MandatoryParameter,
        [Parameter()]
        [AllowNull()]
        [string] $OptionalStringParameter = $null
    )

    $OptionalStringParameter = $null;

    if ($null -eq $OptionalStringParameter) {
        Write-Host -ForegroundColor Green 'This works as expected';
    } else {
        Write-Host -ForegroundColor Red 'Damit - Parameter should be NULL';
    }
}
</code></pre>