範圍

變數的預設範圍是封閉容器。如果在指令碼或其他容器之外,那麼範圍是 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