title: learning powershell and trying to retrieve value from textbox
body: <p>I'm new to this and I'm trying to build a Powershell GUI with a textbox field and use the text to query AD. Something is wrong with my code. The text is stored in variables <code>$textbox_HelloWorld</code>.Text and is called in the event handler <code>$button_ClickMe.Add_Click.</code> This does not return any results. If I replace the variable with a string then it does work. It sounds like a scope issue but I've tried using <code>$script:</code> and <code>$global</code> but neither work. </p>
  
  <p>Any help appreciated.</p>
  
  <pre><code># import AD module
  import-module activedirectory
  
  # Load required assemblies
  [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  
  
  # Drawing form and controls
  $Form_HelloWorld = New-Object System.Windows.Forms.Form
      $Form_HelloWorld.Text = "Hello World"
      $Form_HelloWorld.Size = New-Object System.Drawing.Size(480,240)
      $Form_HelloWorld.FormBorderStyle = "FixedDialog"
      $Form_HelloWorld.TopMost = $true
      $Form_HelloWorld.MaximizeBox = $false
      $Form_HelloWorld.MinimizeBox = $false
      $Form_HelloWorld.ControlBox = $true
      $Form_HelloWorld.StartPosition = "CenterScreen"
      $Form_HelloWorld.Font = "Segoe UI"
  
  
  # adding firstName TextBox to my form
  $textbox_HelloWorld = New-Object System.Windows.Forms.TextBox
      $textbox_HelloWorld.Location = New-Object System.Drawing.Size(8,4)
      $textbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
      $textbox_HelloWorld.TextAlign = "MiddleCenter"
      $global:textbox_HelloWorld.Text = "Enter first name!"
      $Form_HelloWorld.Controls.Add($textbox_HelloWorld)
      $firstnme=$textbox_HelloWorld.Text
  
  
  # adding firstName label to my form
  $label_HelloWorld = New-Object System.Windows.Forms.Label
      $label_HelloWorld.Location = New-Object System.Drawing.Size(128,4)
      $label_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
      $label_HelloWorld.TextAlign = "MiddleCenter"
      $label_HelloWorld.Text = "First Name"
      $Form_HelloWorld.Controls.Add($label_HelloWorld)
  
  # adding lastName TextBox to my form
  $Secondtextbox_HelloWorld = New-Object System.Windows.Forms.TextBox
      $Secondtextbox_HelloWorld.Location = New-Object System.Drawing.Size(8,38)
      $Secondtextbox_HelloWorld.Size = New-Object System.Drawing.Size(120,32)
      $Secondtextbox_HelloWorld.TextAlign = "MiddleCenter"
      $global:Secondtextbox_HelloWorld.Text = "Enter last name!"
      $Form_HelloWorld.Controls.Add($Secondtextbox_HelloWorld)
      $lastnme=$Secondtextbox_HelloWorld.Text
  
  
  # adding lastName label to my form
  $Secondlabel_HelloWorld = New-Object System.Windows.Forms.Label
      $Secondlabel_HelloWorld.Location = New-Object System.Drawing.Size(128,38)
      $Secondlabel_HelloWorld.Size = New-Object System.Drawing.Size(80,16)
      $Secondlabel_HelloWorld.TextAlign = "MiddleCenter"
      $Secondlabel_HelloWorld.Text = "Last Name"
      $Form_HelloWorld.Controls.Add($Secondlabel_HelloWorld)
  
  # add a button
  $button_ClickMe = New-Object System.Windows.Forms.Button
      $button_ClickMe.Location = New-Object System.Drawing.Size(8,80)
      $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
      $button_ClickMe.TextAlign = "MiddleCenter"
      $button_ClickMe.Text = "Click Me!"
      $button_ClickMe.Add_Click({
          $button_ClickMe.Text = "Submitted"
          $nme = Get-ADUser -Filter "GivenName -like '$global:textbox_HelloWorld.Text' -and Surname -like '$global:Secondtextbox_HelloWorld.Text*'" | Select-Object -ExpandProperty SamAccountName | out-string
          [System.Windows.Forms.MessageBox]::Show($nme , "My Dialog Box")
      })
  
      $Form_HelloWorld.Controls.Add($button_ClickMe)
  
  # show form
  $Form_HelloWorld.Add_Shown({$Form_HelloWorld.Activate()})
  [void] $Form_HelloWorld.ShowDialog()
  </code></pre>