Created
February 2, 2016 11:42
-
-
Save tormodfj/fa59cd430a3ed7e625da to your computer and use it in GitHub Desktop.
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 Get-Calendar { | |
<# | |
.SYNOPSIS | |
Prints a calendar for the provided month and year | |
.DESCRIPTION | |
Provided with a month number and a year, prints a month calendar with week starting on monday. | |
.NOTES | |
Author : Tormod Fjeldskår | |
.EXAMPLE | |
C:\PS> Get-Calendar 2 2016 | |
Prints the calendar for February 2016 | |
.PARAMETER month | |
The number of the month to print. | |
.PARAMETER year | |
The year of the month to print. | |
#> | |
param ( | |
[Parameter(Mandatory=$true)] | |
[int]$month, | |
[Parameter(Mandatory=$true)] | |
[int]$year | |
) | |
$date = Get-Date -Day 1 -Month $month -Year $year | |
## Print month name and year as heading | |
$heading = $date.ToString("MMMM yyyy", [System.Globalization.CultureInfo]::InvariantCulture) | |
$headingPadding = (20 - $heading.Length) / 2 | |
Write-Host "" | |
Write-Host "".PadLeft($headingPadding) -NoNewline | |
Write-Host $heading -ForegroundColor Yellow | |
Write-Host "Mo Tu We Th Fr Sa Su" -ForegroundColor Gray | |
## Pad first week with spaces if necessary | |
$firstWeekDayNumber = ($date.DayOfWeek + 6) % 7 # Because normal people consider Monday first day of week | |
$firstWeekDayPadding = $firstWeekDayNumber * 3 | |
Write-Host "".PadLeft($firstWeekDayPadding) -NoNewline | |
## Print days as long as month is the same | |
while($date.Month -eq $month) { | |
if($date.DayOfWeek -eq [System.DayOfWeek]::Sunday) { | |
Write-Host ("" + $date.Day).PadLeft(2) -ForegroundColor Red | |
} | |
else { | |
Write-Host ("" + $date.Day).PadLeft(2).PadRight(3) -NoNewline | |
} | |
$date = $date.AddDays(1) | |
} | |
## Provide whitespace before next command | |
Write-Host "" | |
Write-Host "" | |
} | |
Set-Alias cal Get-Calendar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment