I have a new task. Review some old code written in the time of (Windows) PowerShell 2.0 ensuring I can support it, in case that’s ever needed.
Well, I started last week. While I haven’t been able to get back to it just yet, as some other work came up, I did code a potential change. First off, before you see this code, I didn’t write the original myself. Second, the person that did, would probably do if differently now, too. I’m not here to dis on anyone’s five-year-old code. It’s quite old, in this industry.
Here’s the code as I found it:
$Today = Get-Date $DateArray = (Get-Date $Today.AddDays(-59) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-58) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-57) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-56) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-55) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-54) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-53) -Format "M-d-yyyy"),(Get-Date $Today.AddDays(-46) -Format "M-d-yyyy")
It’s a bit tough to follow. Let me use the included commas as line breaks. Do keep in mind that we can always move to the next line after a comma, and not interrupt our command. It already looks better; it’s much easier on the eyes, at minimum.
$Today = Get-Date $DateArray = (Get-Date $Today.AddDays(-59) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-58) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-57) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-56) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-55) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-54) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-53) -Format "M-d-yyyy"), (Get-Date $Today.AddDays(-46) -Format "M-d-yyyy")
Even though the readability is better, we can still clean this up some more. Before we do that however, let’s discuss what this code does. It essentially creates eight, string dates. Yesterday’s date was October 15, 2018. Based on that date, this code is designed to create string dates from dates in the past. It creates a date string from 58 days ago, 57 days ago, 56 days ago, 55, 54, 53, and even 46 days ago. We’ll run the code now, and return the $DateArray variable.
PS > $DateArray 8-17-2018 8-18-2018 8-19-2018 8-20-2018 8-21-2018 8-22-2018 8-23-2018 8-30-2018
Before I show you what I came up with to change this code, I do want to remind everyone that just because you can simplify something, doesn’t mean you always should. This is especially true if it’s going to make the next person to see your code wonder what the hell you did. I don’t think I’ve done that in this case.
In the below code, we set things up in a ForEach-Object loop. We iteratively send in the numeric values of -59 though -53, and -46. You’ll see that these same values were used in the above example. Inside our loop, we’ll run the Get-Date command subtracting the number of days, based on the number submitted to the loop on that iteration. Additionally, we format the date such as we did originally.
$DateArray = -59..-53 + -46..-46 | ForEach-Object { Get-Date -Date (Get-Date).AddDays($_) -Format 'M-d-yyyy' }
As you can see below, we get the exact same output as we did above. We, simplified the code in such as way that it’s easier to follow visually and intuitively. There’s no speed improvement, as best I could tell, but I’ll still take what I’ve written over what was written years and years ago.
PS > $DateArray 8-17-2018 8-18-2018 8-19-2018 8-20-2018 8-21-2018 8-22-2018 8-23-2018 8-30-2018
Until next time, and for me at least, back to these two PowerShell projects!