@GitHub Copilot - Welcome @BanterBoy, I’m your Copilot and I’m here to help you get things done faster. I can identify issues, explain and even improve code.

You can ask generic questions, but what I’m really good at is helping you with your code. For example:

Generate unit tests for my code Explain the selected code Propose a fix for the bugs in my code If you want to learn more about my capabilities and limitations, check out the Copilot documentation.

I’m powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

@BanterBoy: - Explain what the script does and why it is useful.

@GitHub Copilot -


Function Get-LastxOfMonth {

        [parameter(ParameterSetName = 'ByDate',

        [parameter(ParameterSetName = 'ByString',
        [ValidateRange(1, 12)]

        [parameter(ParameterSetName = 'ByString',

        $asDate = $false
    Begin {
        $alldays = @()
    Process {
        # Validate the Day string passed as parameter by casting it into
        if (-not([System.DayOfWeek]::$Day -in 0..6)) {
            Write-Warning -Message 'Invalid string submitted as Day parameter'

        Switch ($PSCmdlet.ParameterSetName) {
            ByString {
                # Do nothing, variables are already defined and validated
            ByDate {
                $Month = $Date.Month
                $Year = $Date.Year
        # There aren't 32 days in any month so we make sure we iterate through all days in a month
        0..31 | ForEach-Object -Process {
            $evaldate = (Get-Date -Year $Year -Month $Month -Day 1).AddDays($_)
            if ($evaldate.Month -eq $Month) {
                if ($evaldate.DayOfWeek -eq $Day) {
                    $alldays += $evaldate.Day
        # Output
        if ($asDate) {
            Get-Date -Year $Year -Month $Month -Day $alldays[-1]
        else {
    End {}


# Find the last  Wednesday of each month of 2013
1..12 | ForEach-Object -Process {
    Get-LastxOfMonth -Day Wednesday -Month $_ -Year 2013 -asDate

# Make a mistake in the spelling of the day name
Get-LastxOfMonth -Day FridayZ -Month 10 -Year 2010

# Get the last Sunday of this month
Get-LastxOfMonth -Day Sunday -Month 10 -Year 2012

# Find the last Friday of the current month but next year
Get-LastxOfMonth -Day Friday -Date (Get-Date).AddYears(1) -asDate

# Submit a Datetime object through the pipeline
(Get-Date).AddMonths(-4).AddYears(1) | Get-LastxOfMonth -Day Friday -asDate

# Use the other parameter set that uses only a value from the pipeline by property name
New-Object -TypeName psobject -Property @{
    Month = 10;
    Year = 2013;
} | Get-LastxOfMonth -Day Friday -asDate


Back to Top


Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.

Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.


Back to Top