Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about the importance of formatting your Windows PowerShell code, returning objects, and other stuff.
Microsoft Scripting Guy, Ed Wilson, is here. It is Sunday in Charlotte, North Carolina. I have been busy grading scripts since early this morning. The last event, Event 10, appeared on April 13, but there is still one more week during which time contestants are turning in their final scripts. This is the time when the leaderboard will begin to dramatically shift. So I still cannot talk about issues from the last five events, but I can talk about some of the earlier events.
I am not talking about being really crazy and making sure that everything lines up perfectly. I had a professor in college who used to take a ruler to the green bar paper we had to turn in, and he subtracted 1 point for each space that was not perfectly aligned. Regardless of how the code ran, it had to look perfect—in fact; it had to print out perfectly. I am NOT talking about that at all.
But, keep in mind that a fundamental concept is that you should be able to read and understand your script. If you can do that, you reduce the amount of troubleshooting by at least 50% (and no, I have not actually measured that—it is more of a guess). It is in my Windows PowerShell Best Practices book in the chapter about troubleshooting scripts.
So, what am I talking about when I talk about neatness? Here are a few quick guidelines:
It is best if you return an object. This is because anyone who uses your code can pipe the results to other cmdlets, such as Where-Object, Sort-Object, or even Format-Table or Out-GridView. But they are not required to do so. If you spend a lot of time in your script creating your own custom formatting, the person who uses your code cannot process it further. When you send something to Format-Table, Format-List, or Format-Wide, you are done. No further processing can be done because you no longer have the original object—you have a series of formatting objects that behave completely differently than the original object. When you return an object, you permit others to use your code in the way that they want to use it. It makes the eco-system better.
If my boss asks me to write a script that does x, y, and z, and I spend two days creating a really cool script that does a, b, and c, my boss is not going to be too happy. Because although he now has a script that does a, b, and c, he still needs a script to do x, y, and z. Part of developing solutions involves first analyzing the requirements, determining the inputs and the outputs from the script, examining use case scenarios, and finally, beginning to write the code. In fact, in many projects, the actual coding time is far less than the other tasks put together.
Well, that is about all I have time for today. I have a lot of scripts to grade. Take care, and I will see you tomorrow when I will have expert solutions to Beginner and Advanced Events 1.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Great information! I have learned so much in the past week (when I finally decided to try the games)!
A screenshot of unreadable vs readable code would work wonders with this article. :)
Rich, just look up my submissions and you'll probably see examples of what not to do. :)