In This Chapter
Modularizing code using functions
Controlling visibility using scopes
Making functions globally available
Creating your own command using Advanced Functions
It's very rare for any of us to do something only once, especially when it has anything to do with computers. When writing your Windows PowerShell scripts, you'll eventually find that you repeat code over and over. Although that's good exercise for your fingers, it really is counterproductive. Modularizing your code into functions not only saves you tons of time, but also makes your code more robust by making sure that tried-and-tested code is reused so that you don't have to rewrite everything from scratch every time and possibly introduce unnecessary errors.
In this chapter, you harness the real power of scripting by creating reusable code blocks called functions (and their slightly upgraded version, Advanced Functions). Functions allow you to accomplish a lot without writing lots of repetitive code, and often functions makes troubleshooting or making changes to existing scripts a lot easier. So if you're looking for ways to save you more time in the future, keep reading!
Imagine a simple task such as displaying the words I want a nice juicy steak right now on the screen. You already know that you use
Write-Host to display text. If your task is to display that text 100 times in a row, you also know that you can put the text in a loop. This ...