What To Do / Not to Do in PowerShell: Part 11
It can be tempting to modify the global scope to store things like configuration variables. Please do so with great caution.
July 26, 2011
If you've been scripting in PowerShell for a while, you've doubtless run across its concept ofscope. Run help about_scope if you haven't.
The best practice here is to almost never modify the global scope. Doing so is like walking into your neighbor's yard to do something filthy. It's just wrong.
For example, suppose you're building a script module, and you need to share some data between several of the commands (functions) in it. A script-level variable, $script:MyInfo for example, will do the job nicely. No need to go global.
Global variables should be created for one reason only: To expose a global configuration setting - something that a shell user will need to modify. Avoid modifying any of the existing global variables - that's like walking into your neighbor's house and changing not only his TV channel, but also his thermostat setting.
About the Author
You May Also Like