Working with dates in PowerShell revisited

A few years ago, I wrote an article here at TechGenix in which I talked about performing date-related functions in PowerShell scripts. For those who might have missed that original article, I demonstrated a few date-formatting techniques and showed some tricks for performing date comparisons.

Recently, someone asked me a really thought-provoking question about the article. The person who contacted me indicated that they have a PowerShell script that has some specific dates that are hardcoded into the script. They wanted to know how to modify the script so that it would always use the same month and day but would reference the current year rather than having the year hardcoded. As it stands right now, this organization has to remember to modify the script at the beginning of each year due to the hardcoded date.

So, let’s take a closer look at the script. The static dates that I mentioned earlier correspond to the start of the company’s fiscal year and to the dates on which each fiscal quarter begins. These dates are exactly the same from one year to the next, but having the year hardcoded is problematic for obvious reasons.

There are two main approaches to making the year dynamic. The approach that you would use in this type of situation depends on whether you want to have a result that is in date format or a result that is in string format. The difference lies in what you can do with the data. A string is just a block of text. A date that is in date format, however, can be manipulated based on various calendar related functions. For example, you could add 30 days to the date. I will show you both techniques.

PowerShell dates using the string approach

The person who contacted me about the static date problem sent me a portion of their PowerShell script, and the script is designed to treat dates as text strings. Thankfully, PowerShell makes it really easy to make adjustments to text strings.

Here is a portion of the script in its current form. As you can see, the dates are being turned into text strings and then assigned to a variable:

$startQ1date = (Get-Date 2019-2-01).toString("yyyy-M-dd")
$startQ2date = (Get-Date 2019-5-01).toString("yyyy-M-dd")
$startQ3date = (Get-Date 2019-8-01).toString("yyyy-M-dd")
$startAnnualdate = (Get-Date 2019-12-01).toString("yyyy-M-dd")

Since the dates are in a string format, all we have to do is to replace the first four characters of the string with the current year. The first step in this task is to write the current year to a variable. Here is the command that I would recommend using:

$Year = (Get-Date -UFormat %Y)

As noted in my previous article, the Get-Date -UFormat switch determines the format in which the date is displayed. There are placeholders for all of the various elements that make up a date, but %Y corresponds to the year. Since I am only specifying %Y and nothing else, my year variable will contain only the year. You can see what this looks like in the screenshot below.

PowerShell dates
The next thing that has to be done is to create strings based on the concatenation of the static dates (the month and day) and the $Year variable. In the case of the code that is shown above, this could be accomplished like this:

$startQ1date = $Year + "-2-01"
$startQ2date = $Year + "-5-01"
$startQ3date = $Year + "-8-01"
$startAnnualdate = $Year + "-12-01"

You can see the results shown in the screenshot below.

PowerShell datesDate approach

Now that I have shown you how to solve the problem through string manipulation, let’s make things a bit more interesting by putting the results into date format rather than string format. As previously mentioned, the advantage of doing this is that it makes it far easier to perform date-based calculations. I will show you how to do that as well.

The easiest way to create results in date format is to start with the code that we already have but make a couple of minor changes. Here is the code that I will be using:

$Q1 = $Year + "-02-01"
$Q2 = $Year + "-05-01"
$Q3 = $Year + "-08-01"
$Annual = $Year + "-12-01"

As you can see, I have made two changes to the code. The first change was to change the names of the variables. The only reason why I did this was because I needed some temporary variables that I could use in the assembly of the date strings prior to converting the results into date format.

The second change that I made to the code was to change the month into a two-digit value. For example, I am using 02 instead of just 2.

Aside from the changes that I just mentioned, this code does exactly the same thing as the previously demonstrated block of code. Now, however, I need four more lines of code to convert the results into date format. Here are those lines of code:

$startQ1date = [DateTime] $Q1
$startQ2date = [DateTime] $Q2
$startQ3date = [DateTime] $Q3
$startAnnualdate = [DateTime] $Annual

You can see what the whole thing looks like in the screenshot below.

PowerShell dates
Initially, the results look exactly the same as what I showed you before, aside from formatting. However, we can easily change the formatting by using the same technique that you saw at the beginning of the article. Here is an example:


You can see what this looks like below.

So, what about those date calculations that I mentioned earlier? Suppose that for whatever reason, I needed to know a date that was 30 days from the start of Q1. I could find the date by entering this command:


It’s also possible to go back in time by using a negative number. You can see examples of both in the next screenshot.

So. as you can see, PowerShell makes it easy to do date-based calculations.

Featured image: Shutterstock

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top