title: PowerShell SendKeys to InternetExplorer ComObject
body: <p>Here's one method I found which brings the IE ComObject window to the foreground and uses SendKeys.</p>
  
  <p><strong>How would I send a sequence of key presses using this method?</strong></p>
  
  <pre class="lang-psh prettyprint-override"><code>$ie = New-Object -ComObject InternetExplorer.Application
  $ie.navigate("www.google.com")
  do {sleep 1} until (-not ($ie.Busy))
  Add-Type -AssemblyName System.Windows.Forms
  Add-Type @"
   using System;
   using System.Runtime.InteropServices;
   public class StartActivateProgramClass {
  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
   public static extern bool SetForegroundWindow(IntPtr hWnd);
   }
  "@
  $hWnd = $ie.HWND
  $ie.Visible = $true
  [void] [StartActivateProgramClass]::SetForegroundWindow($hWnd)
  [System.Windows.Forms.SendKeys]::SendWait("a 2")
  </code></pre>