title: How can I obtain the live state of the checkbox e.g. checkboxesVM.checked in powershell?
body: <p>I have the code below working perfectly to display the listed folders and the specific VM's in each folder.  The problem comes when I try to find out the checkedstate for each of the checkboxes - the active UI gives me a good checkmark, but nowhere can I find a value/variable I can reference to do two things initially - one, to mark/unmark all the VM's under each Folder if the Folder is checked/unchecked and two, to execute the desired actions - which in this case will include poweroff/poweron support.</p>
  
  <p>Sorry about the size - if you want just the core of the checkbox array - it is in the checkbox region.  The button1 gives me the ability to see which of the checkboxes has been checked because I am looping through the checkboxes but that does not give me active control over the checkboxes.
  The script works great in layout but I cannot seem to find the variable references to obtain the state of the checkboxes or checkboxesVM can anyone tell me how to do this inside the UI so they report their active state in the listbox?  Once I have that I think I can get it working for the stuff I need.</p>
  
  <pre><code>$ServerList = Get-VM | Sort-Object -Property Folder,Name | `
  ?{$_.PowerState -eq "PoweredOn"}
  $ServerCount= $ServerList.Count
  $VMFolders = $ServerList | Group-Object folder
  
  #----------------------------------------------
  Function GenerateForm {
  $handler_button1_Click= {
  #Click Event Monitor
  $listBox1.Items.Clear()
  foreach($x in $checkboxes) {
      $names = $x.name
      $chkStat = $x.Checkstate
      $status = $names+ " " +$chkStat
      $listBox1.Items.Add("$status")
      }
  }
  #end Click Event
  
  $form1 = New-Object System.Windows.Forms.Form
  $button1 = New-Object System.Windows.Forms.Button
  $checkBoxes = New-Object System.Windows.Forms.CheckBox
  $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  
  #region Generated Form Code
  $Form1.Text = "VMWare VM Server Power On/Off"
  $form1.DataBindings.DefaultDataSourceUpdateMode = 0
  $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
  $form1.icon=$icon
  if($ServerCount -ge 6){
      $Form1ShapeW = (1000)
      if($ServerCount -ge 20){
          $Form1ShapeH = ($ServerCount * 35)/5
          } else {
          $form1ShapeH = (120)
      }
  } else {
      $Form1ShapeW = 100 + (155 * $ServerCount)
  }
  $System_Drawing_Size = New-Object System.Drawing.Size($Form1ShapeW,$Form1ShapeH)
  $form1.ClientSize = $System_Drawing_Size
  $form1.autosize = $true
  #endregion
  
  $x=5
  $y=30
  $i=0
  $FX=100
  $FY=$y
  
  #region CheckBoxes
      $Checkboxes = @()
      $VMfolders | %{
      $Checkboxes += New-Object System.Windows.Forms.CheckBox
      $Checkboxes[-1].useVisualStyleBackColor = $true
      $System_Drawing_Size = New-Object System.Drawing.Size(85,25)
      $Checkboxes[-1].Size = $System_Drawing_Size
      $Checkboxes[-1].text = $_.name
      if($x -ge 200 ) {
          $X = 5
          $Y = $y + 27
          $FY = $y
      }
      $System_Drawing_Point = New-Object System.Drawing.Point($x,$y)
      $Checkboxes[-1].Location = $System_Drawing_Point
      $Checkboxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0
      $Checkboxes[-1].name = "folder"
      $y = $y + 27
  
  #region CheckBoxesVM
      $VMList = $_.Group
      $CheckboxesVM = @()
      $VMList | %{
      $CheckboxesVM += New-Object System.Windows.Forms.CheckBox
          $System_Drawing_Size = New-Object System.Drawing.Size(150,25)
          $CheckboxesVM[-1].Size = $System_Drawing_Size
          $CheckboxesVM[-1].text = $_
          if($fx -ge 1000 ) {
              $FX = 100
              $FY = $FY + 25
          }
          $System_Drawing_Point = New-Object System.Drawing.Point($FX,$FY)
          $CheckboxesVM[-1].Location = $System_Drawing_Point
          $CheckboxesVM[-1].DataBindings.DefaultDataSourceUpdateMode = 0
          $CheckboxesVM[-1].name = "server"
          $FX = $FX + 155
          $form1.Controls.Add($CheckboxesVM[-1])
          $y=$FY + 27
      }
      $FX = 100
      $FY = $FY + 27
      #endregion checkboxesVM
  
      $form1.Controls.Add($Checkboxes[-1])
  }
  #endregion Checkboxes
  
  #region Buttons &amp; Boxes
  $button1.TabIndex = 1
  $button1.Name = "button1"
  $System_Drawing_Size = New-Object System.Drawing.Size(75,23)
  $button1.Size = $System_Drawing_Size
  $button1.UseVisualStyleBackColor = $True
  $button1.Text = "Run Script"
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 25
  $z = if($y -ge $FY){
      ($y + 27) }
  else {
      ($FY + 27)
  }
  $System_Drawing_Point.Y = $z
  $button1.Location = $System_Drawing_Point
  $button1.DataBindings.DefaultDataSourceUpdateMode = 0
  $button1.add_Click($handler_button1_Click)
  $form1.Controls.Add($button1)
  $listBox1 = New-Object System.Windows.Forms.ListBox
  $listBox1.FormattingEnabled = $True
  $System_Drawing_Size = New-Object System.Drawing.Size(400,60)
  $listBox1.Size = $System_Drawing_Size
  $listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
  $listBox1.Name = "listBox1"
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = ($x+100)
  $System_Drawing_Point.Y = $y
  $listBox1.Location = $System_Drawing_Point
  $form1.Controls.Add($listBox1)
  }
  #End GenerateForm Function
  #Call the Function
  GenerateForm
  </code></pre>