Creating "Static" Variables

by Oct 15, 2010

Static variables are accessible everywhere. They can be used to collect data from various scopes in one place. You can use a prefix for a variable with "script:" to create a static variable. This example shows a recursive call that runs 10 nest levels. You can use a static variable to keep track of nest level:

Function test {
$script:nestlevel += 1
If ($script:nestlevel -gt 10) { break }
"I am at nest level $script:nestlevel"
Test
}
test

Your static variable will also affect the console if you replace the prefix "script:" by "global:."

Twitter This Tip!
ReTweet this Tip!