I would like to know the scope of the variable $crazy
in the following PowerShell script:
function Test1 {
Write-Host $crazy
}
function Main {
$crazy = Read-Host "Type anything"
Test1
}
Main
I was expecting $crazy
to be only visible within Main
function but, to my surprise, the function Test1
was able to access it. I can guarantee that there's no previously declared $crazy
variable in my PS session.
I'll be honest I didn't fully understand about_scope so I used ISE to debug through the script and tested the scope of the variable with the following pattern $<scope>:$crazy
and the variable didn't even appear every scope I've used.
In this case $crazy
is scoped to the Main function, so it is available inside of Main
and as such available to any function that is called by Main
. If you were to attempt to call $crazy
from outside of Main
you'd find that it was not yet set. Does that help?