How to properly close Internet Explorer when launched from PowerShell? -
there set of asked questions doing internet explorer via powershell. of them contain codes launch ie powershell object, via $ie=new-object -comobject internetexplorer.application
. problem is, proper way of closing ie consists of calling $ie.quit()
not work - first, if ie have happened have more single open tab, ie doesn't quit whole , tab corresponds com object closed, , second, should have 1 tab, window gets closed processes remain.
ps > get-process iexplore handles npm(k) pm(k) ws(k) vm(m) cpu(s) id processname ------- ------ ----- ----- ----- ------ -- ----------- 352 31 7724 24968 142 0,58 3236 iexplore 228 24 22800 15384 156 0,19 3432 iexplore
i have tried research methods on how close process started via new-object -comobject
, , have found this: how rid of comobject. example of comobject excel.application
, indeed behaves intended - calling quit()
makes window close, , executing [system.runtime.interopservices.marshal]::releasecomobject($ex)
if $ex
created comobject stops excel process. not case internet explorer.
i have found question: how existing com object of running ie provides code connect ie via list of open windows, , works extent of ie launched elsewhere, if com object created via powershell, script not able stop ie's processes, if modified such:
$shellapp = new-object -comobject "shell.application" $shellwindows = $shellapp.windows() ($i = 0; $i -lt $shellwindows.count; $i++) { if ($shellwindows.item($i).fullname -like "*iexplore.exe") { $ie = $shellwindows.item($i) $ie.quit() [system.runtime.interopservices.marshal]::releasecomobject($ie) } }
in case of ie launched outside of powershell, processes stopped, in case of ie launched within powershell, 2 processes remain, , code reports have found no ie windows reach com objects, therefore ie processes (yet) unable stopped.
so, how reach apparently orphaned windowless ie processes , gracefully stop them? aware of get-process iexplore | stop-process
, stop , ies, not launched script, , if script run administrator or system
on a, say, remote desktop server, everyone's ies stopped.
environment: os windows 7 x64, powershell 4 (installed above ps version 2), ie11 version 11.0.9600.17691 (automatically updated). ie set "open home page" upon starting, @ least 1 tab open.
simply calling quit()
method should suffice gracefully terminating internet explorer processes, regardless of whether created running iexplore.exe
or instantiating com object in powershell.
demonstration:
ps c:\> $env:processor_architecture amd64 ps c:\> (get-wmiobject -class win32_operatingsystem).caption microsoft windows 8.1 enterprise ps c:\> get-process | ? { $_.processname -eq 'iexplore' } ps c:\> $ie = new-object -com 'internetexplorer.application' ps c:\> get-process | ? { $_.processname -eq 'iexplore' } handles npm(k) pm(k) ws(k) vm(m) cpu(s) id processname ------- ------ ----- ----- ----- ------ -- ----------- 352 20 4244 14164 176 0.05 3460 iexplore 407 32 6428 23316 182 0.23 5356 iexplore ps c:\> $ie.quit() ps c:\> get-process | ? { $_.processname -eq 'iexplore' } ps c:\> _
if have orphaned internet explorer processes don't have handle can cycle through them this:
(new-object -com 'shell.application').windows() | where-object { $_.name -like '*internet explorer*' } | foreach-object { $_.quit() }
to totally on safe side can release com object after calling quit()
, wait garbage collector clean up:
(new-object -com 'shell.application').windows() | where-object { $_.name -like '*internet explorer*' } | foreach-object { $_.quit() [runtime.interopservices.marshal]::releasecomobject($_) } [gc]::collect() [gc]::waitforpendingfinalizers()
Comments
Post a Comment