title: Foreground browser window
body: <p>I'm trying to write a script that can do a few basic keyboard and mouse events, and then 'open' the current Chrome window, and continue from there.</p>
  
  <p>The only problem is that I cannot find a way to bring the Chrome window to the foreground. The code works with simple things, such as Notepad, but it simply cannot find Chrome, no matter how many variations of the name I've tried putting in.</p>
  
  <p>I've looked around the web and couldn't find a solution to this problem.</p>
  
  <p>My current code:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Tricks {
      [DllImport("user32.dll")]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool SetForegroundWindow(IntPtr hWnd);
  }
  "@
  sleep -sec 2
  $h = (Get-Process Chrome).MainWindowHandle
  [void] [Tricks]::SetForegroundWindow($h)
  </code></pre>
  
  <p>If I use <code>Get-Process Chrome</code> this error is thrown:</p>
  
  <pre>
  Cannot convert argument "hWnd", with value: "System.Object[]", for
  "SetForegroundWindow" to type "System.IntPtr": "Cannot convert the
  "System.Object[]" value of type "System.Object[]" to type "System.IntPtr"."
  At line:12 char:1
  + [void] [Tricks]::SetForegroundWindow($h)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
  </pre>
  
  <p>If I use any thing else, this error is thrown:</p>
  
  <pre>
  Get-Process : Cannot find a process with the name "Google Chrome". Verify the
  process name and call the cmdlet again.
  At line:11 char:7
  + $h = (Get-Process "Google Chrome").MainWindowHandle
  +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ObjectNotFound: (Google Chrome:String) [Get-Process], ProcessCommandException
      + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
  </pre>
  
  <p>along with the previous error.</p>
  
  <p>However, if I use something like "Notepad", no error is thrown and the program works as expected.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I've even managed to find a shortened version of the script:</p>
  
  <pre class="lang-bsh prettyprint-override"><code>(New-Object -ComObject WScript.Shell).AppActivate((get-process Notepad).MainWindowTitle)
  </code></pre>
  
  <p>However, as before, this works with something like Notepad, but the moment I give try to use "Chrome" I get this:</p>
  
  <pre>
  Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
  At line:1 char:1
  + (New-Object -ComObject WScript.Shell).AppActivate((get-process Chrome ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  </pre>
  
  <p>The same goes for Firefox.</p>