You can use scope modifiers</em> or the *-Variable</span></code> cmdlets.

The scope modifiers are:

  • global</span></code> used to access/modify at the outermost scope (eg. the interactive shell)</li>
  • script</span></code> used on access/modify at the scope of the running script (.ps1</span></code> file). If not running a script then operates as global</span></code>.</li>
    </ul>
    (For the -Scope</span></code> parameter of the *-Variable</span></code> cmdlets see the help.)

    Eg. in your second example, to directly modify the global $array</span></code>:

    &</span> {</span>
    $global</span>:</span>array </span>+=</span>"s"</span>
    Write</span>-</span>Host</span> $array
    </span>}</span></code></pre>
    For more details see the help topic about_scopes</span></u></a></i>.