Skip navigation

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.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish