title: IE COM automation: How to get the return value of `window.execScript` in PowerShell
body: <p>From <a href="https://stackoverflow.com/q/2000371/450913">this other post</a> it looks like it is possible to get the return value of <code>IHTMLWindow2::execScript</code> from Internet Explorer API, however the linked example is in C++ and I don't really understand it. I'm trying to do the same thing in <strong>PowerShell</strong>, so here's my code that shows the problem:</p>
  
  <pre><code>$ie = New-Object -COM InternetExplorer.Application
  $ie.visible = $true
  $ie.navigate("http://google.com")
  while ( $ie.busy ) { start-sleep -m 100 }
  $document = $ie.document
  $window = $document.parentWindow
  
  Function eval($jsCommand) {
      # Of course it doesn't return anything here. How can I get the return value?
      return $window.execScript($jsCommand, 'javascript')
  }
  
  eval 'parseInt("12")' # returns nothing
  </code></pre>
  
  <p>What I'm really trying to do is to make jQuery selector/objects available in my PowerShell script, so that I can do things like:</p>
  
  <pre><code>eval '$("input#selector")' | where name -eq 'username'
  </code></pre>
  
  <p>and more.</p>
  
  <p><strong>Update:</strong> Look at this <a href="https://gist.github.com/omidkrad/fbb4bfb3b1f5b83c9898" rel="nofollow noreferrer">Gist</a> for PowerShell function to run JavaScript/JQuery and return results back to PS, with timeout. It was extended from the answer below.</p>