How to Get the Last Business Day of the Month Using PowerShell?

One of my team members asked about getting the last business day of the month using PowerShell. The requirement is tricky, but different methods exist to achieve this. In this tutorial, I will show you how to get the last business day of the month using PowerShell.

To get the last business day of the month using PowerShell, you can create a function that starts with the last day of the month and iterates backward until it finds a weekday. Here’s a quick example:

function Get-LastBusinessDay {
    param (
        [int]$Year = (Get-Date).Year,
        [int]$Month = (Get-Date).Month
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -eq 'Saturday' -or $lastDay.DayOfWeek -eq 'Sunday') {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}

This script calculates the last day of the month and checks if it falls on a weekend, adjusting the date backward until a weekday is found.

PowerShell Get Last Business Day of Month

Now, let me show you different methods to get the last business day of the month using PowerShell.

Method 1: Basic Calculation Without Holidays

The simplest way to get the last business day of the month is to start with the last day of the month and work backward until you find a weekday. Here’s how you can do it:

function Get-LastBusinessDay {
    param (
        [int]$Year = (Get-Date).Year,
        [int]$Month = (Get-Date).Month
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -eq 'Saturday' -or $lastDay.DayOfWeek -eq 'Sunday') {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}

# Example usage
$lastBusinessDay = Get-LastBusinessDay -Year 2024 -Month 9
Write-Output $lastBusinessDay

This script defines a function Get-LastBusinessDay that takes a year and a month as parameters. It calculates the last day of the month and then uses a while loop to check if the day is a weekend. If it is, it subtracts one day until it finds a weekday.

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

powershell get last business day of month

Check out Check If Date Is Older Than 30 Days in PowerShell

Method 2: Considering in US Holidays

To make our script more robust, we should account for US holidays. This requires maintaining a list of holidays and modifying our function to check against this list.

Here is the complete PowerShell script to get the last business day of the month, considering the US holidays.

function Get-LastBusinessDayWithHolidays {
    param (
        [int]$Year = (Get-Date).Year,
        [int]$Month = (Get-Date).Month,
        [array]$Holidays
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -eq 'Saturday' -or $lastDay.DayOfWeek -eq 'Sunday' -or $Holidays -contains $lastDay) {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}

# Example usage
$usHolidays = @(
    [datetime]::new(2024, 1, 1),  # New Year's Day
    [datetime]::new(2024, 7, 4),  # Independence Day
    [datetime]::new(2024, 12, 25) # Christmas Day
    # Add other holidays as needed
)
$lastBusinessDay = Get-LastBusinessDayWithHolidays -Year 2024 -Month 9 -Holidays $usHolidays
Write-Output $lastBusinessDay

In this function, Get-LastBusinessDayWithHolidays, we include an additional parameter for holidays. The while loop now also checks if the current date is in the list of holidays and continues to subtract days until it finds a valid business day.

Here is the exact output in the screenshot below:

get last business day of month using PowerShell

Read Compare Dates Without Time in PowerShell

Method 3: Using External Libraries

Various external libraries are also available in PowerShell to handle business days and holidays more elegantly. One such module is BusinessDays, which can be installed from the PowerShell Gallery.

Install-Module -Name BusinessDays -Scope CurrentUser

Import-Module BusinessDays

# Example usage
$holidays = @(
    [datetime]::new(2024, 1, 1),
    [datetime]::new(2024, 7, 4),
    [datetime]::new(2024, 12, 25)
)

$calculator = New-BusinessDaysCalculator -Holidays $holidays
$lastBusinessDay = $calculator.GetLastBusinessDayOfMonth([datetime]::new(2024, 9, 1))
Write-Output $lastBusinessDay

The BusinessDays module simplifies the process by providing built-in functions to calculate business days while considering holidays. This can save you a lot of time and effort, especially if you have a complex holiday schedule.

Conclusion

In this tutorial, I explained how to get the last business day of the month in PowerShell using different methods and how to use the BusinessDays external libraries to work with business days and holidays in PowerShell. Do you still have any questions? Feel free to leave a comment below.

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.