How to Get the Last Friday of the Month Using PowerShell?

If you’re working with dates in PowerShell, you might find requirements like determining the last Friday of a given month. In this tutorial, I will explain how to find the last Friday of the month using PowerShell with examples.

To find the last Friday of the month using PowerShell, you can create a custom function called Get-LastFridayOfMonth. This function takes two parameters: the year and the month. It calculates the last day of the given month and iterates backward day by day until it finds a Friday. Here’s a concise example:

function Get-LastFridayOfMonth {
    param (
        [int]$Year,
        [int]$Month
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -ne 'Friday') {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}

$lastFriday = Get-LastFridayOfMonth -Year 2024 -Month 9
Write-Output "The last Friday of September 2024 is $lastFriday"

Get the Last Friday of the Month Using PowerShell

There are various methods to get the last Friday of a month using PowerShell. Let me explain with examples.

Method 1: Using a Custom Function

The best way to find the last Friday of the month in PowerShell is by creating a custom function. Below is a detailed example of how you can achieve this:

Syntax

function Get-LastFridayOfMonth {
    param (
        [int]$Year,
        [int]$Month
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -ne 'Friday') {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}

This function takes two parameters: the year and the month. It first calculates the last day of the given month and then iterates backward day by day until it finds a Friday.

Example

Here is an example.

function Get-LastFridayOfMonth {
    param (
        [int]$Year,
        [int]$Month
    )
    $lastDay = [datetime]::new($Year, $Month, [datetime]::DaysInMonth($Year, $Month))
    while ($lastDay.DayOfWeek -ne 'Friday') {
        $lastDay = $lastDay.AddDays(-1)
    }
    return $lastDay
}
$lastFriday = Get-LastFridayOfMonth -Year 2024 -Month 9
Write-Output "The last Friday of September 2024 is $lastFriday"

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

powershell get last friday of the month

Check out Get Day of Month in PowerShell

Method 2: Inline Calculation

You can perform the calculation inline if you prefer not to define a function. Here’s how you can do it:

Syntax

$year = 2024
$month = 9
$lastDay = [datetime]::new($year, $month, [datetime]::DaysInMonth($year, $month))
while ($lastDay.DayOfWeek -ne 'Friday') {
    $lastDay = $lastDay.AddDays(-1)
}
$lastFriday = $lastDay

This script follows the same logic as the custom function but is written inline for quick, one-off use.

Example

Now, let me show you a few examples.

$year = 2024
$month = 9
$lastDay = [datetime]::new($year, $month, [datetime]::DaysInMonth($year, $month))
while ($lastDay.DayOfWeek -ne 'Friday') {
    $lastDay = $lastDay.AddDays(-1)
}
$lastFriday = $lastDay
Write-Output "The last Friday of September 2024 is $lastFriday"

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

get last friday of the month in powershell

Read Get the Last Business Day of the Month Using PowerShell

Method 3: Using Get-Date

You can also use the built-in Get-Date cmdlet to assist in finding the last Friday of the month. This method is a bit more concise and leverages PowerShell’s date manipulation capabilities.

Syntax

$year = 2024
$month = 9
$lastDay = Get-Date -Year $year -Month $month -Day ([datetime]::DaysInMonth($year, $month))
while ($lastDay.DayOfWeek -ne 'Friday') {
    $lastDay = $lastDay.AddDays(-1)
}
$lastFriday = $lastDay

This script uses Get-Date to generate the last day of the month and then adjust backward to find the last Friday.

Example

For this, let me show you an example.

$year = 2024
$month = 9
$lastDay = Get-Date -Year $year -Month $month -Day ([datetime]::DaysInMonth($year, $month))
while ($lastDay.DayOfWeek -ne 'Friday') {
    $lastDay = $lastDay.AddDays(-1)
}
$lastFriday = $lastDay
Write-Output "The last Friday of September 2024 is $lastFriday"

You can see the exact output in the screenshot below:

How to get last friday of the month in powershell

Conclusion

In this tutorial, I explained how to find the last Friday of the month using PowerShell using different methods. If you still have questions, then leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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