PowerShell function to return Bank Holidays

Microsoft Windows
Post Reply
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

PowerShell function to return Bank Holidays

Post by Simon Sheppard »

A PowerShell function to return UK Bank Holidays.

Code: Select all

function Get-BankHolidaysUK
  {
       param (
            [int]
            $Year = (Get-Date).Year,

            [ValidateSet("england-and-wales", "scotland", "northern-ireland")]
            [string]
            $country = 'scotland'
       )

       $url = 'https://www.gov.uk/bank-holidays.json/'

       $bankholidays = $(invoke-webrequest -uri "https://www.gov.uk/bank-holidays.json" -UseBasicParsing).content | `
       ConvertFrom-Json | Select-Object -expandproperty $country | `
       Select-Object -expandproperty events | `
       Select-Object date,title | where {($_.date).substring(0,4) -eq $Year}

       $bankholidays
  }
https://ss64.com/ps/syntax-bankhols.html
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: PowerShell function to return Bank Holidays

Post by Simon Sheppard »

French Bank holidays

Code: Select all

function Get-FrenchHoliday
{
    param
    (
        [int]
        $Year = (Get-Date).Year,

        [ValidateSet("alsace-moselle", "guadeloupe", "guyane", "la-reunion", "martinique", "mayotte", "metropole", "nouvelle-caledonie", "polynesie-francaise", "saint-barthelemy", "saint-martin", "saint-pierre-et-miquelon", "wallis-et-futuna")]
        [string]
        $Area = 'metropole',

        [switch]
        $NextOnly
    )

    $url = "https://calendrier.api.gouv.fr/jours-feries/$Area/$Year.json"

    $holidays = Invoke-RestMethod -Uri $url -UseBasicParsing

    foreach ($obj in $holidays.PSObject.Properties) {
        if (-Not ($NextOnly.IsPresent) -or (((([DateTime]$obj.Name).Ticks) - (Get-Date).Ticks) -gt 0)) {
            Write-Host "$($obj.Value) : $($obj.Name)"
        }
    }
}
https://blog.idera.com/database-tools/p ... -holidays/
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: PowerShell function to return Bank Holidays

Post by Simon Sheppard »

German bank holidays

Code: Select all

function Get-GermanHoliday
{
    param
    (
        [int]
        $Year = (Get-Date).Year,

        [ValidateSet("BB","BE","BW","BY","HB","HE","HH","MV","NATIONAL",
                     "NI","NW","RP","SH","SL","SN","ST","TH")]
        [string]
        $State = 'NATIONAL'
    )


    $url = "https://feiertage-api.de/api/?jahr=$Year"

    $holidays = Invoke-RestMethod -Uri $url -UseBasicParsing
    $holidays.$State
} 
https://blog.idera.com/database-tools/p ... -holidays/
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: PowerShell function to return Bank Holidays

Post by Simon Sheppard »

There are some APIs e.g. Nager.date which work for multiple countries, but I don't know how quickly they get updated compared to Govt. supplied APIs.
Post Reply