How to Add Months to Date in PowerShell?

Someone recently tried to add months to the current date in PowerShell. In this tutorial, I will explain how to add months to date in PowerShell with examples.

To add months to a date in PowerShell, you can use the AddMonths method of the DateTime object. For example, to schedule a follow-up meeting three months from today, you can use the following script:

$today = Get-Date
$followUpMeeting = $today.AddMonths(3)

This script takes the current date and adds three months to it, outputting the new date.

AddMonths() method in PowerShell

The AddMonths method is a part of the DateTime object in PowerShell. This method allows you to add a specified number of months to a date. It’s particularly useful for scheduling tasks, setting reminders, or managing time-sensitive data.

Syntax

The syntax for the AddMonths method is:

$date.AddMonths(int months)

Here, $date is a DateTime object, and months is an integer representing the number of months you want to add. If you want to subtract months, you can pass a negative integer.

Here is an example:

$today = Get-Date
$followUpDate = $today.AddMonths(2)
$followUpDate

I executed the above PowerShell script, and you can see the output in the screenshot below:

powershell date add months

Check out PowerShell Get-Date Minus 1 Day

PowerShell Date Add Months Examples

Now, let me show you a few real examples of using AddMonths method in PowerShell.

Example 1: Schedule a Meeting

Imagine you need to schedule a follow-up meeting three months from today. Here’s how you can do it using PowerShell.

$today = Get-Date
$followUpMeeting = $today.AddMonths(3)
Write-Output "The follow-up meeting is scheduled for $($followUpMeeting.ToString('MMMM dd, yyyy'))"

This script gets the current date, adds three months to it, and then outputs the new date in a readable format. If today is September 19, 2024, the output will be:

The follow-up meeting is scheduled for December 19, 2024

Here is the exact output in the screenshot below:

add months to powershell date

Read PowerShell Date Comparison

Example 2: Set a Subscription Renewal Date

Let’s say you manage subscriptions and need to set a renewal date six months from the initial subscription date. Here’s how:

$subscriptionStartDate = Get-Date -Year 2024 -Month 9 -Day 19
$renewalDate = $subscriptionStartDate.AddMonths(6)
Write-Output "The subscription renewal date is $($renewalDate.ToString('MMMM dd, yyyy'))"

If the subscription started on September 19, 2024, the renewal date will be:

The subscription renewal date is March 19, 2025

Here is the exact output in the screenshot below:

Add Months to Date in PowerShell

Read Get Date and Time in 24-Hour Format in PowerShell

Example 3: Calculate Past Dates

You can also use the AddMonths method to calculate dates in the past. For instance, if you want to find out what the date was six months ago:

$today = Get-Date
$sixMonthsAgo = $today.AddMonths(-6)
Write-Output "The date six months ago was $($sixMonthsAgo.ToString('MMMM dd, yyyy'))"

If today is September 19, 2024, the output will be:

The date six months ago was March 19, 2024

Example 4: Handle End-of-Month Scenarios

One thing to note is how the AddMonths method handles end-of-month scenarios. For example, adding one month to January 31 will result in February 28 or 29, depending on whether it’s a leap year.

$date = Get-Date -Year 2024 -Month 1 -Day 31
$newDate = $date.AddMonths(1)
Write-Output "One month from January 31, 2024, is $($newDate.ToString('MMMM dd, yyyy'))"

For January 31, 2024, the output will be:

One month from January 31, 2024, is February 29, 2024

This is because 2024 is a leap year.

Here is the exact output you can see in the screenshot below:

Add Months to Current Date in PowerShell

Conclusion

The AddMonths method in PowerShell can be used to add or subtract months. In this tutorial, I explained how to add months to date in PowerShell and also shown a few real examples of using AddMonths() method in PowerShell.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.