Scopes

PowerShell scopes protect access to variables, aliases, functions, and PowerShell drives (PSDrives) by limiting where they can be read and changed.

The basic rules of scope:

PowerShell Scopes

Global The scope that is in effect when PowerShell starts. Variables and functions that are present when PowerShell starts have been created in the global scope. This includes automatic variables and preference variables.
This also includes variables, aliases, and functions that are in your PowerShell profile.
Local The current scope. The local scope can be the global scope or any other scope.
Script The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
Private Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope.
Numbered Scopes A numeric scope describes the relative position of one scope to another. Scope 0 represents the current, or local, scope. Scope 1 indicates the immediate parent scope. Scope 2 indicates the parent of the parent scope, and so on. Numbered scopes are useful if you have created many recursive scopes. Numbered scopes are relative unlike the named scopes above which are absolute.

A new scope is automatically created by running a script or function, creating a session, or by starting a new instance of PowerShell.
The result is a parent scope (the original scope) and a child scope. Unless you explicitly make the items private, the items in the parent scope are available to the child scope. However, items that you create and change in the child scope do not affect the parent scope, unless you explicitly specify the scope when you create the items.

The default scope for scripts is the script scope.
The default scope for functions and aliases is the local scope, even if they are defined in a script.

Inheritance

A child scope does not inherit the variables, aliases, and functions from the parent scope.
Unless an item is private, the child scope can view the items in the parent scope. And, it can change the items by explicitly specifying the parent scope, but the items are not part of the child scope.

However, a child scope is created with a set of items. Typically, it includes all the aliases that have the AllScope option, plus all the variables that have the AllScope option, plus some variables that can be used to customize the scope, such as MaximumFunctionCount.

To find the items in a particular scope, use the Scope parameter of Get-Variable or Get-Alias.
For example, to get all the variables in the local scope:
PS C:\> get-variable -scope local

To get all the variables in the global scope:
PS C:\> get-variable -scope global

Create or access items in any scope

To specify the scope of a new variable, alias, or function, prefix the name with a scope modifier:

$global:DemoVar = "hello"
$script:DemoVar = "world"
$private:DemoVar = "world"
function global:DemoFunc {...}

Because the local scope is the default, these commands both do the same thing:
$local:DemoVar = "hello"
$DemoVar = "hello"

To read a variable from a different scope, again prefix with a scope modifier:

$global:DemoVar

AllScope

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.

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.

For more about scopes see help about_scopes

“Health is a crown that only the sick can see” ~ Arabic proverb

Related PowerShell Commands:

Dot Sourcing - persist variables and functions after a script (or scriptblock) ends.
& Call Operator - execute a command, script or function.


Copyright © SS64.com 1999-2019
Some rights reserved