Chapter 4. Best practices and optimization

 

This chapter covers

  • Finding code samples
  • Securing the PowerShell environment
  • PowerShell best practices
  • WMI best practices

We’ve had a good look at PowerShell and WMI in the last two chapters. In this chapter, we’ll start to examine how you can use them to solve your administrative problems. We’re not quite at the point of jumping into lots of scripts, though. That treat is reserved for part 2 of the book, which starts with the next chapter.

As the title suggests, this chapter is about best practices. There are a lot of views on what constitutes best practices, ranging from a loose collection of tips and tricks to a rigid set of commandments that should be followed to the letter. My approach falls somewhere between those two extremes, but closer to the loose collection of ideas. I’ll be presenting a collection of techniques that I’ve used or collected over the last five or six years of using PowerShell (that’s going back into the early PowerShell v1 betas). I’ll also present a number of advanced techniques. You can adopt these as and when required; they can be of great benefit in streamlining the production of your PowerShell scripts. The idea is that at the end of the chapter, you’ll know how to produce a good script.

4.1. Security

4.2. Optimizing PowerShell code

4.3. Calculated fields, types, and formatting

4.4. Debugging and error handling

4.5. Getting the most from WMI

4.6. Summary