PowerShell Best Practices

I’ve seen several posts on PowerShell best practices, and even read Ed Wilson’s book on the subject. There is some commonality in the lists in the obvious places (verb-noun, output objects, format your code nicely), and some disagreement in other areas (code signing, for example). I also see a great amount of variation in use of aliases and whether or not to name every parameter. Looking at code in various blogs shows yet another view of what common practices are (whether those are “best” or not is another question).

I’ve been thinking about “best practices” for a long time in PowerShell, and I come at it backwards. I’m really a “proof-of-concept” person. I’ve got a background in Mathematics, so my tendency is to implement something to the point where it works (for some value of “works”) and move on. Polishing scripts and focusing on quality has unfortunately been something that I’ve never really invested a lot into.

At work, lately, I’ve started to spend some time (a few days a month) doing PowerShell training, and I’m really enjoying myself. As I’m teaching, though, I’m trying to instill upon my students a love of PowerShell, and the skills they need to implement quality scripts. And to do that, I have to think about what quality means for me.

Fortunately, I recently read Don Jones and Jeffery Hicks’ new book, Learn PowerShell Toolmaking in a Month of Lunches. This book focuses almost entirely on the practice of making powerful, high-quality, reusable functions in PowerShell and I recommend it highly to anyone who uses PowerShell. It is very different from any other PowerShell book in that it isn’t a tutorial on the language or on how to use certain cmdlets to accomplish tasks.

Will all this going around in my head, I’m trying to formulate a list of best practices and I think that there’s a continuum in what should be recommended. Practices range from “required” (use meaningful variable names!) to “likely to start a religious war” (set tabs to 4 spaces, or braces should be on their own line).

Since I’ve already spent this much text just rambling, I’m thinking that it’s too late to actually start listing my thoughts out, but I’ll try to do that in the next few days. I’d really like to hear some community feedback (pro/con) on various ideas, since I know that there will never be a “final list”.

Let me know what you think.

Mike