PDQ.com mobilePDQ.com desktop
Support
How PowerShell can calculate your retirement

How PowerShell can track 401K growth

Jordan Hammond
·

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  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))

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?!?

The magic of PowerShell

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!

*PowerShell Retirement Calculator Script

Don't miss the next post!

401k and Powershell

How To Track 401k Growth With PowerShell - Part 2

Part 2 of how to track 401k growth with PowerShell, with additional code to tack on to our 401k calculator

PowerShell