A Little Current Date Arithmetic in Powershell–#SQLNewBlogger

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

I saw a fun post on Twitter recently asking days until retirement. I wrote this code:

DECLARE @YearsToRetire INT = 11;

I thought that wasn’t bad, but then I wondered, how would I do this in PowerShell? I knew there had to be a way, so I googled and ran into this article. That showed me Get-Date and the AddYears() method. So I tried this:

2022-01-05 10_26_03-C__Users_Steve

That didn’t work well. I ended up storing the date in a variable and then things worked. However, I was bothered. I can pipe and combine things, why didn’t this work?

Then I thought about something. I ought to enclose the Get-Date inside something. I tried parens, like this:


That worked great. As I get more familiar with PoSh, I can figure more things out myself, and quicker.

2022-01-05 10_28_06-C__Users_Steve


Once I figured out the way to do this, I spent about 5 minutes on this post. Actually 6 minutes with this part added.

I took something I figure out and wrote this up quickly. I even added a great line at the end, that I figured it out and my work with the language is paying dividends.

A great little type of post that would highlight your blog.

About way0utwest

Editor, SQLServerCentral
This entry was posted in Blog and tagged , , . Bookmark the permalink.

1 Response to A Little Current Date Arithmetic in Powershell–#SQLNewBlogger

  1. Pingback: Date Math in Powershell – Curated SQL

Comments are closed.