Accessing Internet Explorer

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Accessing Internet Explorer can be useful for obtaining Web content. The usual approach uses a COM object called InternetExplorer.Application like this:

$ie = New-Object -comObject InternetExplorer.Application
$ie.visible = $true
$ie.navigate('http://www.powershell.com')
$ie
$ie.Document

Unfortunately, this approach fails when you use IE with enhanced security (Vista UAC for example). While you can open IE and navigate to the Web page, you will lose access to the document property once the Web site is loaded because the COM object works only for local files. Once you navigate to a Web page, IE launches again with lower privileges, and PowerShell has no way of accessing the new instance.

To work around this issue, you can use yet another COM object called Shell.Application. It returns all Explorer windows, including all open IE windows. All you need do is pick the IE window you need, by specifying a keyword found in its title bar.

The following example launches a Web page and then waits until a browser window opened with the word "PowerShell" in its title bar. Next, the code returns the IE object and accesses the Web page content.

& "$env:programfiles\Internet Explorer\iexplore.exe" 'http://powershell.com'

$win = New-Object -comObject Shell.Application
$try = 0
$ie2 = $null
do {
Start-Sleep -milliseconds 500
$ie2 = @($win.windows() | ? { $_.locationName -like '*PowerShell*' })[0]
$try ++
if ($try -gt 20) {
Throw "Web Page cannot be opened."
}
} while ($ie2 -eq $null)

$ie2.document
$ie2.Document.body.innerHTML

Posted Apr 03 2009, 08:00 AM by ps1

Comments

Tune Up Your PC » Post Topic » UAC - Accessing Internet Explorer wrote Tune Up Your PC » Post Topic » UAC - Accessing Internet Explorer
on 03-27-2010 8:15 PM

Pingback from  Tune Up Your PC  » Post Topic   » UAC - Accessing Internet Explorer

MIhail Stacanov wrote re: Accessing Internet Explorer
on 08-08-2010 1:32 PM

Thanks a lot for this article. How can I take control of this type object: application/x-shockwave-flash. My website is on flash and I should automate loggging in to it. Thank you in advance.

Copyright 2012 PowerShell.com. All rights reserved.