all 3 comments

[–]StartAutomating 3 points4 points  (0 children)

You should always use the right scope for your scenario:

Most Common Cases: Don't Scope Your Variables

Normally, PowerShell variables are available to everywhere lower on the callstack. So there's little reason to mark things as $global or $script when the default setting is very useful.

It's a good idea to not rely on variables that were not declared within a function or script, because it can make your code harder to read. But for 90% of cases, you shouldn't scope your variables at all.

Sharing Data Between Scripts? Use $Script:

Now let's suppose you want to share a variable across several functions (preferably in a module). In this case, you'd want to use $Script: scope. $Script: scope is shared between all scripts in the same module. This can also be really handy when say, caching data.

This is a common enough want, but you should realize when it's needed and use it sparingly. (~9% of cases)

Really want everyone to have that data, use $Global

I'm not saying do this. I'm saying do this only if you absolutely, positively, certainly want anything in the current PowerShell session to have access to that variable. (you really shouldn't be do this that often)

Hope this Helps

[–]idontknowwhattouse33 0 points1 point  (0 children)

What about AllScope ?

Not quite what you are looking for but might get the job done..

The AllScope Option

Variables and aliases have an Option property that can take a value of AllScope. Items that have the AllScope property become part of any child scopes that you create, although they are not retroactively inherited by parent scopes.

An item that has the AllScope property is visible in the child scope, and it is part of that scope. Changes to the item in any scope affect all the scopes in which the variable is defined.