TOPIC about_Scopes SHORT DESCRIPTION Explains the concept of scope in Windows PowerShell and shows how to set and change the scope of elements. LONG DESCRIPTION Windows PowerShell protects access to variables, aliases, functions, and Windows PowerShell drives (PSDrives) by limiting where they can be read and changed. By enforcing a few simple rules for scope, Windows PowerShell helps to ensure that you do not inadvertently change an item that should not be changed. The following are the basic rules of scope: - An item you include in a scope is visible in the scope in which it was created and in any child scope, unless you explicitly make it private. You can place variables, aliases, functions, or Windows PowerShell drives in one or more scopes. - An item that you created within a scope can be changed only in the scope in which it was created, unless you explicitly specify a different scope. If you create an item in a scope, and the item shares its name with an item in a different scope, the original item might be hidden under the new item. But, it is not overridden or changed. Windows PowerShell Scopes Scopes in Windows PowerShell have both names and numbers. The named scopes specify an absolute scope. The numbers are relative and reflect the relationship between scopes. Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles. 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: You can refer to scopes by name or by a number that 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. Parent and Child Scopes You can create a new scope by running a script or function, by creating a session, or by starting a new instance of Windows PowerShell. When you create a new scope, the result is a parent scope (the original scope) and a child scope (the scope that you created). In Windows PowerShell, all scopes are child scopes of the global scope, but you can create many scopes and many recursive scopes. 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. 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. This option is discussed later in this topic. It includes 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, type: get-variable -scope local To get all the variables in the global scope, type: get-variable -scope global Scope Modifiers To specify the scope of a new variable, alias, or function, use a scope modifier. The valid values of a modifier are Global and Script. The syntax for a scope modifier in a variable is: $[<scope-modifier>]:<name> = <value> The syntax for a scope modifier in a function is: function [<scope-modifier>]:<name> {<function-body>} 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. The following command, which does not use a scope modifier, creates a variable in the current or local scope: $a = "one" To create the same variable in the global scope, use the Global scope modifier: $global:a = "one" To create the same variable in the script scope, use the script scope modifier: $script:a = "one" You can also use a scope modifier in functions. The following function definition creates a function in the global scope: function global:Hello { write-host "Hello, World" } You can also use scope modifiers to refer to a variable in a different scope. The following command refers to the $test variable, first in the local scope and then in the global scope: $test $global:test 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. Managing Scope Several cmdlets have a Scope parameter that lets you get or set (create and change) items in a particular scope. Use the following command to find all the cmdlets in your session that have a Scope parameter: get-help * -parameter scope To find the variables that are visible in a particular scope, use the Scope parameter of Get-Variable. The visible parameters include global parameters, parameters in the parent scope, and parameters in the current scope. For example, the following command gets the variables that are visible in the local scope: get-variable -scope local To create a variable in a particular scope, use a scope modifier or the Scope parameter of Set-Variable. The following command creates a variable in the global scope: new-variable -scope global -name a -value "One" You can also use the Scope parameter of the New-Alias, Set-Alias, or Get-Alias cmdlets to specify the scope. The following command creates an alias in the global scope: new-alias -scope global -name np -value Notepad.exe To get the functions in a particular scope, use the Get-Item cmdlet when you are in the scope. The Get-Item cmdlet does not have a scope parameter. Using Dot Source Notation with Scope Scripts and functions follow all the rules of scope. You create them in a particular scope, and they affect only that scope unless you use a cmdlet parameter or a scope modifier to change that scope. But, you can add a script or function to the current scope by using dot source notation. Then, when a script runs in the current scope, any functions, aliases, and variables that the script creates are available in the current scope. To add a function to the current scope, type a dot (.) and a space before the path and name of the function in the function call. For example, to run the Sample.ps1 script from the C:\Scripts directory in the script scope (the default for scripts), use the following command: c:\scripts\sample.ps1 To run the Sample.ps1 script in the local scope, use the following command: . c:\scripts.sample.ps1 When you use the call operator (&) to run a function or script, it is not added to the current scope. The following example uses the call operator: & c:\scripts.sample.ps1 Any aliases, functions, or variables that the Sample.ps1 script creates are not available in the current scope. Restricting Without Scope A few Windows PowerShell concepts are similar to scope or interact with scope. These concepts may be confused with scope or the behavior of scope. Sessions, modules, and nested prompts are self-contained environments, but they are not child scopes of the global scope in the session. Sessions: A session is an environment in which Windows PowerShell runs. When you create a session on a remote computer, Windows PowerShell establishes a persistent connection to the remote computer. ...
magdula294