Last active
October 8, 2021 13:23
-
-
Save cdhunt/035da233842fbfd72dcff33a43b50678 to your computer and use it in GitHub Desktop.
Print a calendar in PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Show-Calendar { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Position = 0)] | |
[int] | |
$Month = (Get-Date).Month, | |
[Parameter(Position = 1)] | |
[int] | |
$Year = (Get-Date).Year, | |
[Parameter()] | |
[switch] | |
$AsObject | |
) | |
$fgB = "`e[30m" | |
$bgW = "`e[47m" | |
$N = "`e[0m" | |
$beginningOfMonth = Get-Date -Year $Year -Month $Month -Day 1 | |
$monthName = $beginningOfMonth.ToString("MMMM") | |
$yearString = $beginningOfMonth.ToString("yyyy") | |
$currentMonth = $beginningOfMonth.Month | |
$currentDay = $beginningOfMonth | |
$monthObject = while ($currentDay.Month -eq $currentMonth) { | |
$currentDay | |
try { | |
$currentDay = Get-Date -Year $Year -Month $Month -Day ($currentDay.Day + 1) | |
} | |
catch { | |
break | |
} | |
} | |
if ($AsObject) { | |
return $monthObject | |
} | |
$title = "$monthName $yearString" | |
$sp = [Math]::Floor((21 - $monthName.Length - 5) / 2) | |
$title.PadLeft((21 - $sp), ' ') | |
"Su Mo Tu We Th Fr Sa" | |
$line = '' | |
foreach ($day in $monthObject) { | |
$dayValue = $day.DayOfWeek.value__ | |
if ($day.Day -eq 1) { | |
$line += ' ' * ($dayValue * 3) | |
} | |
if ($day.Date -eq (Get-Date).Date) { | |
$line += $fgB + $bgW | |
} | |
$line += $day.Day.ToString().PadLeft(2) | |
if ($day.Date -eq (Get-Date).Date) { | |
$line += $N | |
} | |
if ($dayValue -eq 6) { | |
$line += [Environment]::NewLine | |
} | |
else { | |
$line += ' ' | |
} | |
} | |
if ($line[-1..-2] -notcontains "`n") { | |
$line += [Environment]::NewLine | |
} | |
$line | |
} | |
New-Alias -Name cal -Value Show-Calendar -Force |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the date for next [day_of_week] | |
function Next { | |
param ( | |
[ValidateSet('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')] | |
[string] $DayOfWeek, | |
[int] $Weeks = 1 | |
) | |
(Get-Date).Date.AddDays([int][DayOfWeek]$DayOfWeek - (Get-Date).Date.DayOfWeek).AddDays(7 * $Weeks) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment