Often you need to collect a date as a parameter for a script. There are several ways to display a GUI calendar to select the date from. I made a demo script for two of the calendar controls. Both are part of the System.Windows.Forms namespace. The MonthCalendar Class displays in a monthly view. You can move forward or back through the months or click into it and change to a less and less granular view. The DateTimePicker Class defaults to a single line display which can open into the monthly view.

In a simple WinForms form –

Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,8.25"
$mainForm.Text = " Calendar Controls Demo"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkOliveGreen"
$mainForm.Width = 400
$mainForm.Height = 30

I added the MonthCalendar control. I set several properties including minimum and maximum dates displayed, how many dates can be concurrently selected, which day of the week in the first column and several others. This control has a lot of properties to manage it’s presentation.

# MonthCalendar
$monthCal = New-Object System.Windows.Forms.MonthCalendar
$monthCal.Location = "8,24"
$monthCal.MinDate = New-Object System.DateTime(2013, 1, 1)
$monthCal.MinDate = "01/01/2012"    # Minimum Date Dispalyed
$monthCal.MaxDate = "12/31/2013"    # Maximum Date Dispalyed
$monthCal.MaxSelectionCount = 1     # Max number of days that can be selected
$monthCal.ShowToday = $false        # Show the Today Banner at bottom
$monthCal.ShowTodayCircle = $true   # Circle Todays Date
$monthCal.FirstDayOfWeek = "Sunday" # Which Day of the Week in the First Column
$monthCal.ScrollChange = 1          # Move number of months at a time with arrows
$monthCal.ShowWeekNumbers = $false  # Show week numbers to the left of each week

I then added the DateTimePicker control. I set the min and max dates to match the MonthCalendar controls limitations. Not all of the MonthCalendar Properties are available with the DateTimePicker.

# DateTimePicker
$datePicker = New-Object System.Windows.Forms.DateTimePicker
$datePicker.Location = "18,220"
$datePicker.MinDate = "01/01/2013" # Minimum Date Dispalyed
$datePicker.MaxDate = "12/31/2013" # Maximum Date Dispalyed

The Select button will add the selected date to the TextBox for that calendar on the click event. The MonthCalendar and DateTimePicker controls work slightly differently. With MonthCalendar you can select a date range so to get a single date you use the SelectionStart property. While DateTimePicker supports only a single date and so you use it’s Value property.

# Select Button 1
$dateTimePickerButton = New-Object System.Windows.Forms.Button
$dateTimePickerButton.Location = "270,150"
$dateTimePickerButton.Size = "75,23"
$dateTimePickerButton.ForeColor = "DarkBlue"
$dateTimePickerButton.BackColor = "White"
$dateTimePickerButton.Text = "<<<-- Select"
    $textBox.Text = $monthCal.SelectionStart
    $textBox.Text = $textBox.Text.Substring(0,10)


# Select Button 2
$monthlyCalendarButton = New-Object System.Windows.Forms.Button
$monthlyCalendarButton.Location = "270,220"
$monthlyCalendarButton.Size = "75,23"
$monthlyCalendarButton.ForeColor = "DarkBlue"
$monthlyCalendarButton.BackColor = "White"
$monthlyCalendarButton.Text = "<<<-- Select"
    $textBox.Text = $datePicker.Value
    $textBox.Text = $textBox.Text.Substring(0,10)