Understanding Variable Inheritance

by Oct 14, 2010

PowerShell variables are inherited downstream by default, not upstream. So any variable you create at a given scope is passed to any code you call from there:

$a = 1
Function test {
"variable a contains $a"
$a = 2
"variable a contains $a"
}
test
variable a contains 1
variable a contains 2
$a
1

This is why the function test receives variable $a from its parent scope. While it can define its own variable $a as well, it never affects the parent variable $a because it is only inherited downstream.

Twitter This Tip!
ReTweet this Tip!