范围
变量的默认范围是封闭容器。如果在脚本或其他容器之外,那么范围是 Global
。要指定范围 ,它的前缀是变量名称 $scope:varname
,如下所示:
$foo = "Global Scope"
function myFunc {
$foo = "Function (local) scope"
Write-Host $global:foo
Write-Host $local:foo
Write-Host $foo
}
myFunc
Write-Host $local:foo
Write-Host $foo
输出:
Global Scope
Function (local) scope
Function (local) scope
Global Scope
Global Scope