Get-Calendar.ps1 – Shows a month calendar control and returns selected dates

The idea is borrowed from the //o//’s blog //o// PowerShelled: Calendar Function (GUI). It is nice and simple. Perhaps a few touches here make it more customizable and useful for wider range of tasks.


The script works as date(s) viewer/picker. It shows WinForm dialog with a month calendar control with predefined date(s) selected and returns start and end date of initial or changed date range. By default 12 months (3 columns, 4 rows) are shown. Week numbers may be optionally turned on by a switch.


# Select dates and get number of days including the end date
$dates = Get-Calendar
($dates[1] - $dates[0]).TotalDays + 1

# Use predefined range, two months view and week numbers shown
$start, $end = Get-Calendar '2006.10.16' '2006.11.16' 2,1 -week
Write-Host "Start: $start. End: $end."


## Author   : Roman Kuzmin inspired by //o//
## Synopsis : Shows a month calendar control and returns selected dates
## Modified : 2006.10.15
## Returns an array of two selected start and end dates
## -Start <datetime>: initial start date
## -End <datetime>: initial end date
## -Dimensions <int[]>: (columns, rows) of months
## -WeekNumbers <switch>: show week numbers
## *) Press Enter to return currently selected dates.
## *) Press Escape or close the form to return initial dates.

    [datetime]$Start = [datetime]::Now,
    [datetime]$End = $Start,
    [int[]]$Dimensions = (3, 4),

# load forms
& {[void][System.Windows.Forms.Form]; trap {[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); continue}}

# new calendar
$cal = New-Object System.Windows.Forms.MonthCalendar
$cal.CalendarDimensions = New-Object System.Drawing.Size $Dimensions
$cal.Margin = New-Object System.Windows.Forms.Padding 0
$cal.MaxSelectionCount = 366
$cal.SelectionRange = New-Object System.Windows.Forms.SelectionRange $Start, $End
$cal.ShowWeekNumbers = $WeekNumbers

# new form
$form = New-Object System.Windows.Forms.Form
$form.AutoSize = $true
$form.AutoSizeMode = 'GrowAndShrink'
$form.FormBorderStyle = 'FixedDialog'
$form.KeyPreview = $true
$form.MaximizeBox = $false
$form.Text = 'Calendar'
    switch($_.KeyCode) {
        'Escape' {$form.Close()}
        'Return' {$form.Close(); $Start = $cal.SelectionRange.Start; $End = $cal.SelectionRange.End}

# show and return dates
$Start.Date, $End.Date

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: