What To Do / Not to Do in PowerShell: Part 11

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.

Please or Register to post comments.

What's PowerShell with a Purpose Blog?

Don Jones demystifies Windows PowerShell.

Blog Archive

Sponsored Introduction Continue on to (or wait seconds) ×