Counting the days with PowerShell

Created a script for counting the days until a set date in the future. To see how many days is left to reach a goal or just to find out how many days there is before Christmas. Just for fun.

The main functionality of the script is subtracting two dates. It takes the future date substracted by current date or todays date and store this in a variable and outputting to the console. With some date formatting for instance, $($Today.GetDateTimeFormats()[0]) this uses a .NET method to format the date to dd.mm.yyyy. Just to see the date of todays date and the date in the future at the same time in the output. The script looks like the following:

function Get-DaysToBigGoal {
    [CmdletBinding()]
    param (
        # The date of the desired goal
        [Parameter(Mandatory = $true)]
        [string]
        $GoalDate,

        # The current date, todays date.
        [Parameter(Mandatory = $false)]
        [DateTime]
        $Today = (Get-Date)
    )
    begin {
        $Goal = Get-Date -Date $GoalDate
    }
    process {
        $DaysToGo = $Goal - $Today
        Write-Host -ForegroundColor Green "Total days from today $($Today.GetDateTimeFormats()[0]), until $($Goal.GetDateTimeFormats()[0]) is $($DaysToGo.Days) days"
    }
}

To run the script, you must load the script into memory and then run the cmdlet with cmdlet below. In the example below I tested with how many days until Christmas, and from the time of writing it is 80 days left. Have a lot to look forward to this date.

PS C:Get-DaysToBigGoal -GoalDate 24.12.2019
Total days from today 04.10.2019, until 24.12.2019 is 80 days

That’s for it today.

Have an awesome day!