How to close IE sessions opened by Invoke-WebReque

2019-05-14 09:25发布

问题:

I am noticing in my script that Invoke-WebRequest is launching Internet Explorer (presumably to parse the DOM). Is there a way to close down these IE sessions at the end of my script, for cleanup purposes?

The line highlighted in red is the iwr which prompts Internet Explorer to open. If I add -UseBasicParsing to that $AllRegs request, the script fails a couple lines later, trying to capture $LibraryID

回答1:

To forcefully close all IE sessions use:

get-process iexplore | stop-process

To more gracefully close IE sessions use:

Get-Process iexplore | ForEach-Object { $_.CloseMainWindow() }

You can also add a wait after the ForEach call, but I use the same line for shutting Chrome down & it has never been necessary.