0

PowerShell 5.1

I'm not sure I understand what just happened in the script below:

function Main {
    $testParm = 'hello world'
    function1
}
function function1 {
    $testParm | Out-Host
}
Main

Output

hello world

I'm evaluating $testParm within Function1 which should be blank. But it's outputting 'hello world' which was assigned only in Main function.

Rod
  • 14,529
  • 31
  • 118
  • 230
  • 1
    As explained in the linked answer, if you want `$testParm` to not be visible by `function1` use the `private:` scope modifier. – Santiago Squarzon Mar 09 '23 at 19:33
  • Wt?! That wasn’t intuitive, smh. I wouldn’t expect that to be a default behavior. – Rod Mar 09 '23 at 19:41
  • 3
    [Unless you explicitly make the items private, the items in the parent scope are available to the child scope.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-5.1#parent-and-child-scopes) – JosefZ Mar 09 '23 at 19:46

0 Answers0