The other day I was reviewing a script that a previous admin who had been in the role before me had written, and it contained this section that I thought was quite humorous:
PowerShell, or any other scripting or programming language, is just that, a language. Everyone has their own mannerisms and ways of speaking, and for the most part can get by just fine. However, just like traditional languages, there can be beautiful and eloquent code; a few words can be incredibly powerful (Though this is not always for good, as anyone who has had a recursive delete get away from them can attest). This often comes with a deep understanding gained through experience, which of course can take quite a while to develop.
Someone might look at the above code block and think it looks just fine (besides the somewhat funny variable name), and indeed when I first started out with programming I would likely agree. What constitutes good scripting is often times a matter of opinion, and in my mind there are a few central tenets, one of the main one’s being:
Code should be as minimal as possible without sacrificing readability
In this minimal example, there’s a few changes we can make. To start off, when assigning a variable, anything to the right of an = sign is put into the variable on the left, so there’s no need to put in the superfluous parenthesis:
$monday = get-date -format dddd if ($monday -like "*TUesday*")
Next, the Get-Date formatting that is used returns the full name of the day. There are a few options here, we could instead use -format ddd which instead returns the 3 letter date (ie. Tue for Tuesday), or alternatively -uformat %u, which would return the day of the week as a number (starting with Sunday at 0, so Tuesday would be 2).
Why would we want to do this? Mostly for less typing later, as well as quicker processing; using the -like operator requires a more computational effort than if we were to use -eq. To switch to use the number of the week day:
$monday = get-date -uformat %u if ($monday -eq 2)
Now for one of the things that I find makes code much more concise, removing the variable entirely! Almost every time you’re using a variable for just a single if statement, it can be replaced by moving what you’d be assigning to the variable directly into the if statement:
if ((get-date -uformat %u) -eq 2)
Note that we had to add the parenthesis back in to make this work, this is where they actually are necessary, since we need to get the date before we can compare it to anything.
This is quite a bit more concise, but does call into question are rule earlier about sacrificing readability. Unless you know exactly what -uformat %u means, it’s a lot easier to figure out what if ($monday -like *TUesday*) means, even if it doesn’t make much sense. Which leads to another central tenet:
Everything should be documented, especially if it’s not immediately clear what something does
So to add in a nice meaningful comment (and do a bit of formatting):
# Get the day of the week as a number, and see if it's Tuesday If ((Get-Date -UFormat %u) -eq 2)
Much better 🙂