Open tab in existing IE instance
Solution 1
You can use Start-Process
to open the URL. If a browser window is already open, it will open as a tab.
Start-Process 'http://www.microsoft.com'
Solution 2
First you have to attach to the already running Internet Explorer instance:
$ie = (New-Object -ComObject "Shell.Application").Windows() |
Where-Object { $_.Name -eq "Windows Internet Explorer" }
Then you Navigate
to the new URL. Where that URL is opened is controlled via the Flags
parameter:
$ie.Navigate("http://www.google.com/", 2048)
Edit: In case 2 or more IE instances are running (additional tabs count as additional instances as well) the enumeration will return an array, so you have to select a particular instance from the array:
$ie[0].Navigate("http://www.google.com/", 2048)
Solution 3
You can use this if Internet Explorer is not your default browser:
Function Open-IETabs {
param (
[string[]]$Url
)
begin {
$Ie = New-Object -ComObject InternetExplorer.Application
}
process {
foreach ($Link in $Url) {
$Ie.Navigate2($Link, 0x1000)
}
}
end {
$Ie.Visible = $true
}
}
I found this on PowerShell.com
Rohit2194017
Updated on February 14, 2020Comments
-
Rohit2194017 over 4 years
$ie = New-Object -com internetexplorer.application
Everytime i open a new website with this object(ie every time when script runs) it is opened in a new IE window and i don't want it to do that. I want it to be opened in a new tab but that too in a previously opened IE window. I want to reuse this object when the script runs next time. I don't want to create a new object
So is there any way like to check for the instances of internet explorer and to reuse its instance ???
I tried this as a solution:
First you have to attach to the already running Internet Explorer instance:
$ie = (New-Object -COM "Shell.Application").Windows() ` | ? { $_.Name -eq "Windows Internet Explorer" }
Then you Navigate to the new URL. Where that URL is opened is controlled via the Flags parameter:
$ie.Navigate("http://www.google.com/", 2048)
but am not able to call
navigate
method on this newly created object$ie
. -
Rohit2194017 over 11 yearsbut m not able to call navigate method on this newly created object,ie. on $ie
-
Ansgar Wiechers over 11 yearsThe IE object is not newly created. How exactly are you "not able to call the
Navigate
method"? Do you get an error? Does the first command return an object at all? What is the output of$ie.GetType()
and$ie | Get-Member
? -
Rohit2194017 over 11 yearsthe first command was successful and it returned the object of the same kind, i already checked it with get-member, everything looks to be fine with the object creation but still m not able to call navigate function, it shows an error which is given below: Method invocation failed because [System.Objec At line:1 char:14 + $ie3.navigate <<<< ("www.google.com") + CategoryInfo : InvalidOperation + FullyQualifiedErrorId : MethodNotFound
-
Rohit2194017 over 11 yearsthe first command was successful and it returned the object of the same kind, i already checked it with get-member, everything looks to be fine with the object creation but still m not able to call navigate function, it shows an error which is given below: Method invocation failed because [System.Object[]] doesn't contain a method named 'navigate'. At line:1 char:14 + $ie3.navigate <<<< ("www.google.com") + CategoryInfo : InvalidOperation: <navigate:string>[], RuntimeException + FullyQualifiedErrorId : MethodNotFound
-
Rohit2194017 over 11 yearsi checked the type of both the objects and it appeared to be of different types: the newly created was of type Object[] and with a base type of System.Array. And the one which was created using without callinig window() method (that is the one which should be created) was of type _ComObject and with a base of System.MarshalByRefObject
-
Ansgar Wiechers over 11 yearsUse
$ie[0].Navigate(...)
. Apparently you have more than one IE instance running, so the enumeration returns an array instead of a single object. -
Rohit2194017 over 11 yearshey yaa, thankzzzz mate, it worked, thanxx a lot :) :) :) :) :)
-
John P about 11 yearsThis assumes that Internet Explorer is the default web browser.
-
jonsinfinity about 11 yearsI'm fairly certain
Start-Process
just sends the URL to whatever the default browser is on the system. It works for me with Firefox as my default browser. -
John P about 11 yearsYes, it does open in the default browser, Chrome, FF, IE or whatever. However the question was focused on IE, so I just wanted to point out that IE has to be set to the default browser.
-
jonsinfinity almost 11 yearsYou are correct. I interpreted your first comment to mean it only worked if IE is the default browser.
-
jonsinfinity over 6 yearsI'd appreciate an explanation of what the downvoter doesn't like about this answer. I know it won't happen, but still...