title: How do I capture the selected value from a ListBox in PowerShell?
body: <p>I am trying to create a Popup asking a user to select a department and then based on the department run a selection of Choco commands to install software each department needs to use. I'm running into an issue with it not returning the selected #var.  </p>
  
  <p>Troubleshooting so far has shown that no matter what item I select in the list it just run the commands under the first IF section and then stop going.</p>
  
  <p>I've tried to scalre down my code to just ask it to print each business department so I could check the list box setup and I can't find what i'm missing.</p>
  
  <pre><code>[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
  
  $objForm = New-Object System.Windows.Forms.Form 
  $objForm.Text = "Department"
  $objForm.Size = New-Object System.Drawing.Size(249,190) 
  $objForm.StartPosition = "CenterScreen"
  
  #OK Button action
  $objForm.KeyPreview = $True
  $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
      {$TSDepartment=$objListBox.SelectedItem;$objForm.Close()}})
  
  #Ok Button
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(10,115)
  $OKButton.Size = New-Object System.Drawing.Size(200,30)
  $OKButton.Text = "OK"
  $OKButton.Add_Click({$TSDepartment=$objListBox.SelectedItem;$objForm.Close()})
  $objForm.Controls.Add($OKButton)
  
  # List box Lable
  $objLabel = New-Object System.Windows.Forms.Label
  $objLabel.Location = New-Object System.Drawing.Size(10,20) 
  $objLabel.Size = New-Object System.Drawing.Size(200,20) 
  $objLabel.Text = "Please select a Department:"
  $objForm.Controls.Add($objLabel) 
  
  #list box Side
  $objListBox = New-Object System.Windows.Forms.ListBox 
  $objListBox.Location = New-Object System.Drawing.Size(10,40) 
  $objListBox.Size = New-Object System.Drawing.Size(200,20) 
  $objListBox.Height = 70
  
  #pick list
  [void] $objListBox.Items.Add("Call-Center")
  [void] $objListBox.Items.Add("Law-Office")
  [void] $objListBox.Items.Add("Admin-Support")
  [void] $objListBox.Items.Add("Base")
  [void] $objListBox.Items.Add("IT")
  
  $objForm.Controls.Add($objListBox) 
  $objForm.Topmost = $True
  $objForm.Add_Shown({$objForm.Activate()})
  [void] $objForm.ShowDialog()
  
  #resulting var
  #$TSDepartment
  
  # Simple PowerShell ElseIf
  # Admin-Support softwre list
  if ($TSDepartment = "Admin-Support")  
  {
  'Admin Support Software'
  choco upgrade chocolatey    
  get-executionpolicy         
  set-executionpolicy remotesigned                        
  
  choco   install 7zip    --ignore-checksum
  choco   install adobereader --ignore-checksum
  choco   install cutepdf --ignore-checksum
  choco   install jre8    --ignore-checksum
  choco   install Silverlight --ignore-checksum
  choco   install vlc --ignore-checksum
  choco   install webex   --ignore-checksum
  
  } 
  
  # Law Office softwre list
  ElseIf ( $TSDepartment = "Law-Office")  
  {
  'Law Office Software'
  choco upgrade chocolatey    
  get-executionpolicy
  set-executionpolicy remotesigned
  
  choco install 7zip --ignore-checksum
  choco install adobereader --ignore-checksum
  choco install cutepdf --ignore-checksum
  choco install vlc  --ignore-checksum
  choco install jre8 --ignore-checksum
  choco install rsclientprint --ignore-checksum
  choco install Silverlight --ignore-checksum
  choco install webex --ignore-checksum
  } 
  
  # Call Center softwre list
  ElseIf ( $TSDepartment = "Call-Center")  
  {
  'Call Center Software'
  choco upgrade chocolatey    
  get-executionpolicy         
  set-executionpolicy remotesigned            
  
  choco   install adobereader --ignore-checksum
  choco   install cutepdf --ignore-checksum
  choco   install jre8    --ignore-checksum
  choco   install Silverlight --ignore-checksum
  choco   install vlc --ignore-checksum
  choco   install webex   --ignore-checksum
  choco   install Silverlight --ignore-checksum
  choco   install softphone   --ignore-checksum
  }
  
  # IT softwre list
  ElseIf ( $TSDepartment = "IT")  
  {
  'IT Software'
  choco upgrade chocolatey    
  get-executionpolicy         
  set-executionpolicy remotesigned            
  
  choco   install 7zip    --ignore-checksum
  choco   install adobereader --ignore-checksum
  choco   install cutepdf --ignore-checksum
  choco   install jre8    --ignore-checksum
  choco   install Silverlight --ignore-checksum
  choco   install vlc --ignore-checksum
  choco   install webex   --ignore-checksum
  choco   install Firefox --ignore-checksum
  choco   install foxitreader --ignore-checksum
  choco   install Ghostscript.app --ignore-checksum
  choco   install GoogleChrome    --ignore-checksum
  choco   install greenshot   --ignore-checksum
  choco   install notepadplusplus --ignore-checksum
  choco   install PowerShell  --ignore-checksum
  choco   install putty   --ignore-checksum
  choco   install sysinternals    --ignore-checksum
  choco   install windirstat  --ignore-checksum
  choco   install wireshark   --ignore-checksum
  }
  
  # Base Software List
  ElseIf ( $TSDepartment = "Base")  
  {
  'Base Software'
  choco upgrade chocolatey    
  get-executionpolicy         
  set-executionpolicy remotesigned            
  
  choco   install adobereader --ignore-checksum
  choco   install cutepdf --ignore-checksum
  choco   install jre8    --ignore-checksum
  choco   install Silverlight --ignore-checksum
  choco   install webex   --ignore-checksum
  }
  
  # null Software List
  Else
  {
  'Upgrade Choco'
  choco upgrade chocolatey    
  get-executionpolicy         
  set-executionpolicy remotesigned            
  }
  </code></pre>