Get-DayOfWeek.ps1


Description

Purpose

Returns the current day of the week, a random day, or a shuffled list of all days.

Detailed Description

This function generates the current day of the week by default. If the -Random switch is provided, it returns a single random day. If the -ShuffleList switch is provided, it returns a shuffled list of all days.

Back to Top

Usage

Example 1

Get-DayOfWeek

Returns the current day of the week, e.g., “Wednesday”.

Example 2

Get-DayOfWeek -Random

Returns a single random day of the week, e.g., “Monday”.

Example 3

Get-DayOfWeek -ShuffleList

Returns a shuffled list of all days of the week, e.g., “Friday”, “Monday”, “Sunday”, etc.

Back to Top

Notes

Author: Luke Leigh Date: April 3, 2025 Version: 1.1 This function uses the Get-Random cmdlet with the -Shuffle parameter for shuffling.

Back to Top


Script

<#
.SYNOPSIS
    Returns the current day of the week, a random day, or a shuffled list of all days.

.DESCRIPTION
    This function generates the current day of the week by default. If the `-Random` switch is provided, 
    it returns a single random day. If the `-ShuffleList` switch is provided, it returns a shuffled list 
    of all days.

.PARAMETER ShuffleList
    If specified, the function returns a shuffled list of all days of the week.

.PARAMETER Random
    If specified, the function returns a single random day of the week.

.EXAMPLE
    Get-DayOfWeek
    Returns the current day of the week, e.g., "Wednesday".

.EXAMPLE
    Get-DayOfWeek -Random
    Returns a single random day of the week, e.g., "Monday".

.EXAMPLE
    Get-DayOfWeek -ShuffleList
    Returns a shuffled list of all days of the week, e.g., "Friday", "Monday", "Sunday", etc.

.NOTES
    Author: Luke Leigh
    Date: April 3, 2025
    Version: 1.1
    This function uses the `Get-Random` cmdlet with the `-Shuffle` parameter for shuffling.

#>
function Get-DayOfWeek {
    [CmdletBinding()]
    param (
        [switch]$ShuffleList,
        [switch]$Random
    )

    # Define the days of the week
    $daysOfWeek = @("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")

    if ($ShuffleList) {
        # Use the -Shuffle parameter to shuffle the list
        return $daysOfWeek | Get-Random -Shuffle
    }
    elseif ($Random) {
        # Return a single random day
        return Get-Random -InputObject $daysOfWeek
    }
    else {
        # Return the current day of the week
        return (Get-Date).DayOfWeek
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply 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.

Issue


Back to Top