Winter Scripting Games 2014 Tip #1: Avoid the aliases

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 Smile), 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:

ls -di | ? {
    $_.LastWriteTime -gt (date).AddMonths(-24)
} | % {
    mv -pat $_.fullname -des C:\Temp -wh 

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!” Winking smile).

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.

Get-ChildItem -Directory | Where-Object {
    $_.LastWriteTime -gt (Get-Date).AddMonths(-24)
} | ForEach {
    Move-Item -Path $_.fullname -Destination C:\Temp -WhatIf 

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.

This entry was posted in powershell, Winter Scripting Games 2014 and tagged , , , . Bookmark the permalink.

5 Responses to Winter Scripting Games 2014 Tip #1: Avoid the aliases

  1. George says:

    Using this same guidance, wouldn’t you expand “Where” to “Where-Object”

  2. rjasonmorgan says:

    So where will the judges/coaches stand on the where with no curly brackets?

    ls | where lastwritetime -ge (get-date 1/1/2014)

    Does it make it more or less readable? Coming from before v3 it seems odd but that may just be because {$_} are so ingrained.

    • Boe Prox says:

      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 😉

  3. Pingback: Winter Scripting Games 2014 Tip #1: Avoid the aliases |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s