title: Powershell 2 works 3 does not
body: <p>Why does the code below return a date in power shell 2 but not in Powers hell 3 I assume it to do with the variable $dtmDate being clear once the form object is ended. but how to I have it return to be used? </p>
  
  <p>Cheers </p>
  
  <pre><code>[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
  
  $objForm = New-Object Windows.Forms.Form 
  
  $objForm.Text = "Select a Date" 
  $objForm.Size = New-Object Drawing.Size @(190,190) 
  $objForm.StartPosition = "CenterScreen"
  
  $objForm.KeyPreview = $True
  
  $objForm.Add_KeyDown({
      if ($_.KeyCode -eq "Enter") 
          {
              $dtmDate=$objCalendar.SelectionStart
              $objForm.Close()
          }
      })
  
  $objForm.Add_KeyDown({
      if ($_.KeyCode -eq "Escape") 
          {
              $objForm.Close()
          }
      })
  
  $objCalendar = New-Object System.Windows.Forms.MonthCalendar 
  $objCalendar.ShowTodayCircle = $False
  $objCalendar.MaxSelectionCount = 1
  $objForm.Controls.Add($objCalendar) 
  
  $objForm.Topmost = $True
  
  $objForm.Add_Shown({$objForm.Activate()})  
  [void] $objForm.ShowDialog() 
  
  if ($dtmDate)
      {
          Write-Host "Date selected: $dtmDate"
      }
  </code></pre>