Having been a judge for the previous 2 Scripting Game competitions as well as competing in the 2 before that, I have seen my share of scripts submitted that didn’t quite meet the cut of what I felt were the best scripts. It doesn’t mean that they wouldn’t work out in the real world in a production environment (Ok, some wouldn’t ), but some were just really hard to read or others were doing things that I wouldn’t consider to be a good practice.

I’m not judging this year and am instead taking on the role as a coach which gives me the great opportunity to provide input on a submission while the event is ongoing which also allows me to blog about what I am seeing to help everyone out. My goal over the course of the next few weeks is to provide some feedback based on the scripts that I have seen as well as bringing up some past things that have hindered some otherwise excellent scripts. Maybe you will agree with me, maybe you won’t. But if anything, it will make you think about what you might be writing and using in your environment.

I will start this little excursion by talking about the use of aliases in scripts. An alias is a shorthand way to run a command or use a parameter in a script/function. An example of this is here:

This is probably a little extreme, but I think you can appreciate what I am trying to point out, which is that aliases make it pretty hard to read the code (especially if you are just learning PowerShell) or if you are trying to read someone else’s code and make sense of the direction that they were going.

If you are just running code ad-hoc from the shell, then this is perfectly fine to do as only you are worried about what is being done and have no plans on giving this to someone else (maybe you are, but you might just say “run this and don’t ask questions!” ).

So back to our little code snippet above. Perfectly fine for a console run, but in a script, this may present a headache for others. Lets clean this up so everything has been expanded out to be readable.

Now I have a better idea as to what is being done in this code snippet. Unless otherwise noted, this is not about the shortest amount of characters that you can use in a script, it is about making the script do what you want it to do as well as making sure it is readable to whoever happens to be looking at it. This not only helps the next person understand more of what is going on, but also aids in the troubleshooting process in case the script doesn’t work properly or maybe more developing is done on it.

I wouldn’t use the simplified syntax in a script because it seems to have a hard time handling complex filters (such as adding a -AND or -OR statements) and I personally feel it is somewhat gimmicky. Just my opinion though😉