ramses1592
ramses1592

Reputation: 550

How to close a tab in existing IE window using powershell

Hi Does anyone know how to open and close a tab in internet explorer using powershell. I am able to open one using the following

 $navOpenInNewTab = 0x800
 # Get running Internet Explorer instances
 $oApp = New-Object -ComObject shell.application
 # Grab the last opened tab
 $oIE = $oApp.Windows() | Select-Object -Last 1
 # Open link in the new tab nearby
 $oIE_Total_Sends = $oIE.navigate($link, $navOpenInNewTab) 
 while ($oIE.busy) {
 sleep -milliseconds 50
 }

How do i close this new tab through powershell and why can't I access Internet Explorer methods like getElementByID on $oIE.Document object.

Upvotes: 1

Views: 3586

Answers (1)

Andy Schneider
Andy Schneider

Reputation: 8684

Here is a function that will close a tab in IE.

Function Close-IETab {
param($url)

    $oWindows = (New-Object -ComObject Shell.Application).Windows

    foreach ($oWindow in $oWindows.Invoke()) {

        if ($oWindow.Fullname -match "IEXPLORE.EXE" -and $oWindow.LocationURL -match $url) {

            Write-verbose "Closing tab $($oWindow.LocationURL)"
            $oWindow.Quit()
        }
    }

}

Upvotes: 3

Related Questions