How Can I Re-Launch Internet Explorer if No Other Instances are Running?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I re-launch Internet Explorer if no other instances are running?

— MT

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MT. Thanks for the question. You know, nowadays everyone picks on poor little Internet Explorer (even though the vast majority of Windows users still use Internet Explorer). But now we have a question from someone who wants to make sure that Internet Explorer is always up and running. See, Internet Explorer: someone still loves you!

As happy as we might have been for Internet Explorer, though, we have to admit that this question put us in a bit of a quandary. After all, there are several different ways to handle this problem, depending on whether you need Internet Explorer to re-start instantly or whether it’s OK to wait awhile. After pondering the matter for awhile we decided to do what we always do: go with the simplest solution, one that (in this example) checks every 60 seconds to see if any instances of Internet Explorer are running. If there are, then the script simply goes back to sleep and waits another 60 seconds before checking again. If there aren’t any instances of Internet Explorer running the script launches a new copy of Internet Explorer, then takes a quick nap and waits another 60 seconds before checking again.

And, yes, that’s somewhat similar to what a Scripting Guys day is like. Um, except for the part about waking up every 60 seconds in order to do something, if you know what we mean.

Here’s the script we came up with:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) Set objShell = CreateObject(“Wscript.Shell”)

Do While True Set colProcesses = objWMIService.ExecQuery _ (“Select * from Win32_Process Where Name = ‘iexplore.exe'”) If colProcesses.Count = 0 Then objShell.Run “iexplore.exe” End If Wscript.Sleep 60000 Loop

That’s right: this is, at heart, just a run-of-the-mill WMI script. Because of that, it starts off by connecting to the WMI service on the local computer. But there is one key difference between this script and other WMI scripts. Usually at this point we say, “But you can also run this script against a remote computer.” Unfortunately, that’s not the case here. Technically you could run the script against a remote computer, but – at least on Windows XP and Windows Server 2003 – any instance of Internet Explorer you started up would run in an invisible window; you wouldn’t be able to see it on the screen. That’s a security feature built into the operating system: processes started remotely always run in a hidden window. Which, in turn, means that this script must be run on the local computer.

Note. Is there a workaround to this problem? As a matter of fact, there is, although it requires you to start the process locally rather than remotely. For an example of how you might do that, see this Hey, Scripting Guy!column.

After connecting to the WMI service we then create an instance of the Wscript.Shell object, which we’ll use to spawn any new instances of Internet Explorer. (And, yes, we could have used WMI for this, but most people find it easier to run programs using Wscript.Shell rather than WMI.) We then set up a Do loop that runs for as long as True is equal to True. (Which, barring any new developments in philosophy, means the script will run forever. To stop the script you’ll need to terminate the script process. If you’re running in a command window under CScript that’s as easy as pressing Ctrl+C or closing the command window.)

So what do we do inside the loop? Well, to begin with, we use this code to retrieve a collection of all the processes named iexplore.exe that are currently running on the computer:

Set colProcesses = objWMIService.ExecQuery _
    (“Select * from Win32_Process Where Name = ‘iexplore.exe'”)

At the risk of spoiling the suspense, that’s going to coincide with a collection of all the instances of Internet Explorer currently running on the computer. We then check to see if the value of the Count property (which tells us how many items are in the collection) is equal to 0:

If colProcesses.Count = 0 Then

If Count is equal to 0 that means there aren’t any instances of Internet Explorer running on the machine. Therefore, we use the Shell object and the Run method to start up a brand-new instance:

objShell.Run “iexplore.exe”

And that’s it. We then use the Sleep method to pause the script for 60 seconds (60,000 milliseconds). When 60 seconds are up, the script will wake up, loop around, and repeat the process, ad infinitum. If 60 seconds isn’t the desired interval you can simply adjust the value. For example, this line of code runs the check every 30 seconds (30,000 milliseconds):

Wscript.Sleep 30000

And this one runs the check every 10 minutes (60,000 milliseconds per minute times 10 minutes):

Wscript.Sleep 600000

Admittedly, that only checks every 10 minutes to see if Internet Explorer is doing something. With the Scripting Guys, there’s no point in checking every 10 minutes to see if they’re doing anything: after all, you already know the answer to that, script or no script.


Discussion is closed.

Feedback usabilla icon