Skip to content

How to use PowerShell to track 401k growth — Part 1

Jordan Hammond fun headshot
Jordan Hammond|November 13, 2019
 How PowerShell can track 401K growth
 How PowerShell can track 401K growth

Everyone has something that drives them. Something that you just gotta have. My obsession is nothing, and I want it bad. I don’t mean that I’m looking for the absence of stuff. I want to wake up every day and have absolutely nothing that I have to do. Sure, I’ll do the things I want to do, but I won’t have to do anything. After researching my long term goals, I’ve discovered I need a lot of stuff to achieve my ultimate nothing. And by stuff, I mean money.

I spend a lot of time looking at various retirement calculators and so far I’m not satisfied with what I’ve found. They all follow the basic math, but none share the math behind their calculators and others make assumptions I don’t like. So, I set out to build my own. As is always the case, I ask myself “Can I do this in PowerShell?” The answer has never changed and is, “of course!” In this blog, I focus on a 401k calculator because its tax advantages make it ideal to build your retirement nest egg. *Also, since this is a long blog, if you want to dive right into the script itself there is a link at the bottom to download it.

First, let’s cover the obvious, I am not a financial adviser. I built this for my own edification. While it may give you an idea of where your 401k will be at your planned retirement age, there are no guarantees. When it comes to your retirement you can never have too much information.

Before we get into the PowerShell let’s get into the math! We need the equation used for solving compound interest. In math it looks like:

C = (1 + r/m)m where

  • C = Compound Interest

  • r= Rate of return

  • m = Number of contributions per year

In PowerShell is looks like:

$rate = [math]::pow((1+ $Results.InterestRate / $Results.Contributions), ($Results.Contributions))

Looking to spice things up a little? Change the PowerShell colors.

It is possible to multiply your current principal, add in the future value of a series, then multiply the exponent by the number of years you are looking to contribute to your 401k. This will give you a final number. We want to track what the new value of your nest egg is by year. So, we are going to grab the value for each year and add it to a graph. Because why would you even number when you can look at a line?!?

Bring on the PowerShell

Let’s get into the PowerShell now! It is 339 lines, but the majority of it was because I chose to use winform for input. The brains of the script is only a couple of lines

$Contributions = $EmployeeContributions + $CompanyContributions $zRate = [math]::pow((1 + $Results.InterestRate / $Results.NumContributionsPerYear), ($Results.NumContributionsPerYear)) $Results.Principal = [math]::Round(([int]$Results.Principal * $zRate) + ($Contributions * ($zRate - 1) / $Results.InterestRate))

This is where the math happens...the magic? 

Or, where the Mathgic happens! 

It takes your existing principal and multiplies it by our compound interest equation. Then, it  adds your contributions to the future value of a series. This becomes your new principal at the end of the year. 

Enough of the words, we demand action! 

Bob Boberson is 35 years old and making 50k a year. He is paid monthly and contributing 12% of his salary to his 401k and his company is matching 4%. He is getting a 3% raise each year and an average growth rate of 6%. How much will Mr. Boberson have at 65?

Who needs word problems, we have pictures!

PowerShell retirement calculator

Bob will have $1,083,928 in his 401k at retirement.

Now in picture format!

PowerShell retirement over time calculations

I hear what you are saying though, why isn’t Bob adding every other raise to his 401k? Doesn't Bob know that he can max out his contributions sooner and retire earlier? Well, Jordan hasn’t added that math to his script yet. Trust me, Bob would if he could.

So, your next question may be, is this enough for Bob to retire? That is its own complex question. One that I would love to dive into next! In the next blog PowerShell will tell us how much Bob needs to retire, and how long it will take him to get there!

Jordan Hammond fun headshot
Jordan Hammond

Jordan had spent his life wondering why tasks he didn’t like to do had no options to complete themselves. Eventually he had to make that happen on his own. It turned out that he enjoyed making tasks complete themselves, and PDQ thought that is something he should talk about on the internet.

Related articles