Killing an Internet Explorer process started by Powershell

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of John Quick John Quick 6 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #36359
    Profile photo of John Quick
    John Quick
    Participant

    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?

    #36360
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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()
    
    #36361

    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

    #36362
    Profile photo of John Quick
    John Quick
    Participant

    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 (https://sharepoint.southstream.info/Pic_Announcement_Icons/users)
    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?

    #36375
    Profile photo of John Quick
    John Quick
    Participant

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.