I'm learning about scopes in Powershell and have some questions:
- About the "Local scope": From what i read, the local scope is always the current scope. So, by default, when we create an item (without scope modifier), e.g. a variable, in some scope, let it be script or global, the scope will be script/global accordingly. So my question is: when will we need to explicitly specify the
local
modifier? - MSDN says:
You can create a new scope by running a script or function, by creating a session, or by starting a new instance of 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). ...
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.
But when i try the following:
PS> $Name = "John"
PS> Powershell.exe
PS>echo $Name // No Output
It seems from the quote above that the "starting a new instance of powershell" is a child scope, so all the items in the parent scope should be visible there. Can someone explain?