I have been just learning the very basics of power shell. From what I understand variables by default are private if declared in a function. I do not understand why $private:var is needed. are the following two functions equivalent in what they do?
function test {
$a = 6 }
function test2 {
$Private:a = 6}
take a look at this example:
function test1 {
$a = 6
write-host "test1: $a"
function inner1 {
write-host "inner1: $a"
$a=7
}
inner1
write-host "test1: $a"
}
function test2 {
$Private:a = 6
write-host "test1: $a"
function inner2 {
write-host "inner2: $a"
$a=7
}
inner2
write-host "test2: $a"
}
test1
test2
inner2 doesn’t print any value because test2 have private variable
I see functions keep stuff private from outside of them by default. To keep stuff private from functions you need the $private: syntax to accomplish this.