I just finished typing out a long question, only to figure out the answer on my own right before I posted. However, it is still not a perfect solution and I am wondering if I could get an explanation why the following won't work.
What I want to do is have an optional parameter, that if not set, will be null. I would like that parameter to be a string.
So I would do something like this:
function myfunction {
param([string]$x = $null)
set-aduser -identity someuser -officephone $x
}
By doing that, it seems like $x can never be null, even if I explicitly set it later in the code. If I were to remove [string], it works fine. Just wondering why that would be. It's not a big deal, but I can't see why it wouldn't work.
[string]type cannot accept a value of$null. This is mentioned in passing in about_functions_advanced_parameters in reference to AllowNull (though the comment about a[string]parameter not taking$nullnormally seems to be wrong at least in some quick testing here).