Variable scope
PowerShell uses scopes to limit access to variables (and other items, such as functions). Scopes are layered one on top of another; child scopes inherit from parent scopes. Parent scopes cannot access variables created in child scopes.
Scopes are also used by PowerShell when optimizing blocks. Local
ly scoped variables can be optimized for performance, but variables from parent scopes cannot be.
There are three named scopes:
Global
Script
Local
Global
is the topmost scope; it is the scope the prompt in the console uses and is available to all child scopes.
The Script
scope, as the name suggests, is specific to a single script. Script
scoped items are available to all child scopes (such as functions) within that script. The Script
scope is also available in modules, making it an ideal place to store variables that should be shared within a module.
Local
is the current scope and is therefore relative. In the console, the...