3

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.

1
  • The [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 $null normally seems to be wrong at least in some quick testing here). Commented May 11, 2015 at 23:16

1 Answer 1

5

This is because you are casting the parameter value to a string:

param([string]$x = $null)
      ^^^^^^^^

Casting $null to a string always returns an empty string:

PS > [string]$x = $null
PS > $x.GetType() 

IsPublic IsSerial Name                                     BaseType                
-------- -------- ----                                     --------                
True     True     String                                   System.Object           


PS > $x.Length
0
PS >

One way to get around this would be to remove the cast and check the type of the variable yourself using the -is operator:

function myfunction {
    param($x)
    if ($x -is [string] -or $x -eq $null) {
        set-aduser -identity someuser -officephone $x
    }
}

You should then probably also output an error if the user passes an argument of the wrong type or else the function will apparently do nothing, which can be confusing.

Buy if you are already doing all this, you should ask yourself if it would not be better to just have Set-ADUser output the error for you. It would be more efficient, involve less code, and ultimately produce the same result.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way around this while keeping the type cast?
Not with casting, no. Casting a null value to a string will always produce a string. See my edit though. You could maybe use the -is operator.
@AnsgarWiechers - Thanks for the edit. Note however that I purposefully left out the =$null because it is unnecessary. PowerShell variables will automatically be assigned to $null if you refer to them without giving them a value beforehand.
Thank you for your responses! I am surprised that I can't do what I want, but I appreciate the explanation!
@iCodez Ah, sorry. You're right, of course. Feel free to roll back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.