Killing an Internet Explorer process started by Powershell

Dear All,

I am using the following code to start an IE process and navigate to a website.

$process = Start-Process "C:\Program Files (x86)\Internet Explorer\iexplore.exe" http://www.nu.nl -PassThru

Then I want to close this process so I use

$process.Kill()

This works beautifully ONLY when one Internet Explorer window is open. If there is already an Internet Explorer window open it will throw the following error

Exception calling "Kill" with "0" argument(s): "Cannot process request because the process has exited."
At line:2 char:1
+ $process.Kill()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

How can I close only the Internet Explorer window that was opened from the Powershell session?

Hmm… my suspicion is that IE wants to have a single process, so when it starts up, it looks for another running instance. If it finds one, it tells that instance to open the webpage you asked for (probably via DDE or some such mechanism) and then closes the new process. That would match what you’ve observed.

You might be able to work around this by using the COM objects instead of Start-Process:

$ie = New-Object -ComObject InternetExplorer.Application
$ie.Visible = $true
$ie.Navigate('http://www.nu.nl')

# then later:

$ie.Quit()

Another alternative is to capture the IE processes BEFORE you programmatically open IE.
$procs = Get-Process iexplore | select -ExpandProperty Id
Start-Process “C:\Program Files (x86)\Internet Explorer\iexplore.exe” http://www.nu.nl

Get-Process iexplore | where ID -notin $procs | Stop-Process

This obviously breaks down if you open other IE processes AFTER your start-process. In that case isolate the processes belonging to your IE instance of interest so you can kill them

Hi Dave,

Thank you for your suggestion. This works good but…
The actual URL that I need to use is an internal sharepoint page.
Whenever I set that URL (Turk Stream - WebService Login)
To start Internet Explorer with it will load IE with that page. However when I use $ie.quit() it shows the following error

The interface is unknown. (Exception from HRESULT: 0x800706B5)
At line:1 char:1
+ $ie.quit()
+ ~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

When I change the URL back to http://www.nu.nl or even httpS://www.google.com it closes the window as expected. Any idea what can be the issue here?

Richard,

Thanks for the help. When I use your code IE will restore itself automatically because it thinks it crashed…
Any idea how to prevent that?

Thank you both for pointing me in the right direction.

I use the below and it works perfectly

Start:

Start-Process iexplore https://sharepoint.southstream.info/Pic_Announcement_Icons/users

Stop

Get-Process iexplore | Foreach-Object { $_.CloseMainWindow() | Out-Null } | stop-process –force

This opens the IE window and navigates to the site. And closes it when with the Stop code part.