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:
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
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