February 2019
Intermediate to advanced
626 pages
15h 51m
English
When creating a variable in the console (outside of functions or script blocks), the local scope is global. The global scope can be accessed from inside a function (child) because it is a parent scope:
Remove-Variable thisValue -ErrorAction SilentlyContinue
$Local:thisValue = "Some value"
"From Local: $local:thisValue" # Accessible
"From Global: $global:thisValue" # Accessible
function Test-ThisScope {
"From Local: $local:thisValue" # Does not exist
"From Global: $global:thisValue" # Accessible
}
Test-ThisScope
When scopes are explicitly named as this, the source of a variable value can be reasonably clear. If the scope prefix is removed, PowerShell attempts to resolve the variable by searching the parent scopes, as ...
Read now
Unlock full access