PowerShell Function to get the last business day from today's date

# Function to get the last business day from today's date

function Get-LastBusinessDay {

    # Get today's date

    $today = Get-Date


    # Determine the last business day based on today's day of the week

    switch ($today.DayOfWeek) {

        'Monday'   { $lastBusinessDay = $today.AddDays(-3) } # Last Friday

        'Sunday'   { $lastBusinessDay = $today.AddDays(-2) } # Last Friday

        'Saturday' { $lastBusinessDay = $today.AddDays(-1) } # Last Friday

        default    { $lastBusinessDay = $today.AddDays(-1) } # Previous day

    }


    # Return the last business day

    return $lastBusinessDay

}


# Get and print the last business day

$lastBusinessDay = Get-LastBusinessDay

Write-Output "The last business day from today's date is: $($lastBusinessDay.ToString('yyyy-MM-dd'))"


Comments

Popular posts from this blog

host

Steps to create SSH key from git bash

test