Detecting param variable input only

This topic contains 3 replies, has 3 voices, and was last updated by  Rob Simmers 3 years, 9 months ago.

  • Author
  • #16298

    Adam Bertram

    I have a script with a standard string param in it. In the script, I want to detect if this string is $null. I've included a script snippet. Rather than using 2 variables, is it possible to set a condition only on the parameter? Something like setting a type of [param] like if ([param]$x) ?

    $somearray = 'product1','product2'
    foreach ($element in $somearray) {
        ## I only want to test against the $x set by the param
        ## I don't want this if $x was set lower in the script
        if (!$x) {
           do something
        $x = 'something'
  • #16299

    Adam Bertram

    Nevermind. Posted too soon. $PSBoundParameters['x'] did the trick.

  • #16300

    Dave Wyatt

    It's not really a good practice to overwrite the value of a parameter variable like that. Technically, though, you could check the value of $PSBoundParameters['x'] to get at the original argument that was passed in. Personally, I would just use two variable names, even if one is originally initialized as a copy of $x.

  • #16301

    Rob Simmers

    Take a look at advanced functions:


You must be logged in to reply to this topic.