title: Powershell: Set focus to child window
body: <p>I am in the processing of Automating Virus Scan Exclusions.  Since I cannot write straight to the registry, I am making a macro of sorts using PowerShell.  It works pretty well, but I want to ensure that I have the focus of the window I want before I send a slew of key strokes to it.</p>
  
  <p>Using the following code, I get to the VirusScan Exclusions for McAfee.</p>
  
  <pre><code>&amp; 'C:\Program Files (x86)\McAfee\VirusScan Enterprise\mcconsol.exe'
  Start-Sleep 5    
  [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
  [Microsoft.VisualBasic.Interaction]::AppActivate("VirusScan Console")
  Start-Sleep -m 500
  [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
  [System.Windows.Forms.SendKeys]::SendWait("{DOWN}{DOWN}{DOWN}%{ENTER}")
  start-sleep -m 500
  #[Microsoft.VisualBasic.Interaction]::AppActivate("On-Access Scan Properties")
  [System.Windows.Forms.SendKeys]::SendWait("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{DOWN}{TAB}{TAB}{TAB}{RIGHT}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{RIGHT}%E")
  start-sleep -m 750
  </code></pre>
  
  <p>However, there is a chance that focus could not be on the correct window.  </p>
  
  <p>Child windows
  'On-Access Scan Properties' and then 'Set Exclusions' (as a child of 'On-Access Scan Properties') appear.</p>
  
  <p>Questions:
     1) What code can I use to set the focus to a particular subwindow (by title)?
     2) What code can I use to test that the window is in fact the focus?</p>
  
  <p>Basically I use and RDP manager to connect to a bunch of different machines.  I want to set this script off and forget it.  In order to do that, I have to ensure that the proper windows are in focus.  It doesn't work well when I am switching between RDP connections.</p>