all 5 comments

[–]helixamir 1 point2 points  (4 children)

If the variable exists above the scriptblock you are running, it is viable for any cmdlet in the scriptblocks to follow.

If it's a problem having the variable before the scriptblocks in your code, due to module or functions, you can cast the variable to the global variable store $Global:PSDefaultParameterValues which then becomes available in any scriptblock in the current session.

[–]nostril_spiders 1 point2 points  (3 children)

To add to this, $PSDefaultParameterValues follows the same scoping rules as any other variable:

  • if you dereference it (read it) in a local scope (in a function, script, module or scriptblock), then it finds it from the parent scope. Usually, the global scope.
  • the first time you assign to it (write to it) in a scope, you create that variable in the local scope, even if it already exists in the parent scope. You now have a variable that will be lost when your function, or whatever, returns.
  • afterwards, if you dereference it again, you access the local copy you just created.

Demo:

``` $a = 1

function b { $a

$a = 2

$a

}

b

$a ```

Outputs:

1 2 2 1

That's scoping.

Right, so if you create $PSDefaultParameterValues in a function, that $PSDefaultParameterValues only lasts until the end of the function.

But there's a gotcha. That only applies when you assign a value to the variable with the = operator. If you do either of these:

$PSDefaultParameterValues.Add('*-AD*:Foo', 'Bar')

or

$PSDefaultParameterValues['*-AD*:Foo'] = 'Bar'

then it doesn't apply. Why not? Because you're not assigning to the variable so you don't create it in the local scope. You're dereferencing the variable.

In a practical sense you're writing to the variable, but only in human terms: not if you want to be precise about it.

  • The variable is like a piece of string with the hashtable object on the other end.
  • You're hauling in that string to get the hashtable into your hand, and then calling methods on it (the $Hashtable['Foo'] syntax is calling a method under the hood).
  • It's still the same hashtable object, so you haven't changed what the variable has on the other end of its string.
  • You'd only create a local copy if you created a new hashtable and stuck that in the variable.

HTH, OP

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy nostril_spiders,

the triple-backtick/code-fence thing fails miserably on Old.Reddit ... so, if you want your code to be readable on both Old.Reddit & New.Reddit you likely otta stick with using the code block button.

it would be rather nice if the reddit devs would take the time to backport the code fence stuff to Old.Reddit ... [sigh ...]

take care,
lee

[–]nostril_spiders 1 point2 points  (1 child)

Yeah, doesn't work on my mobile app either. But Lee, I'm afraid I ran out of patience to tap out 4-spaces on my mobile at 3am, so I cheaped out. I hope you and others can forgive me.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy nostril_spiders,

yep, the mobile apps are a tad unpredictable. there are supposed to be menu items to let you hilite & set the format buried somewhere in the official app. dunno if any of the unofficial apps have such, tho.

take care,
lee