How Can I Disable a Service?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I disable a service?

— DS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DS. You know, the Scripting Guys seem to have stumbled upon a great idea. In our last column, we showed people how to disable the LMHosts file. Today, we’re going to show everyone how to disable a service. If we continue down this path, we’ll soon have shown you how to disable everything on a computer. Just think of that: no more problems with the network, no more help desk calls, no more users overwriting files that shouldn’t have been overwritten. All we have to do is disable everything and all those problems will disappear! This could be our ticket to the big time.

Admittedly, disabling everything on all your computers might introduce some other problems in your organization. But that’s really an internal thing you guys will have to deal with.

Besides, we’ll probably be too busy counting our money to help you with that.

Of course, until that money starts to roll in we still have to write this column and we still have to pay the bills. With that in mind, here’s a script that disables the Alerter service on a computer:

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

Set colServiceList = objWMIService.ExecQuery _ (“Select * from Win32_Service where Name = ‘Alerter'”)

For Each objService in colServiceList If objService.State = “Running” Then objService.StopService() Wscript.Sleep 5000 End If errReturnCode = objService.ChangeStartMode(“Disabled”) Next

As you can see, it doesn’t take much code to disable a service, and the little bit of code that is required is pretty basic. There is one tricky part, however, which is why we’ll take a few minutes to walk you through the script.

Besides, the Rolls Royce dealership doesn’t open for another hour anyway.

There’s nothing too special about the way the script starts: we simply connect to the WMI service on the local computer (although we could also disable a service on a remote computer). We then issue this query, which returns a collection consisting of just one item: the Alerter service:

Set colServiceList = objWMIService.ExecQuery _
    (“Select * from Win32_Service where Name = ‘Alerter'”)

Incidentally, make sure you include a Where clause similar to the one above. Why? Well, suppose you leave the Where clause off. In that case, your collection will consist of all the services installed on the computer, and your script will then dutifully try to disable every single service you have.

You’re right: that’s usually not a good thing. Besides, the Scripting Guys would have to sue you; after all, disabling everything on a computer is our idea!

After we get back our collection, we set up a For Each loop to walk through all the items in that collection. (And yes, we know: there’s only one item in the collection. But we still need to use a For Each loop.) Here’s the tricky part. You can disable a service if it’s running; however, the service will not actually be disabled until it stops. If you disable a running service, the service will continue running the same as ever; the change doesn’t take effect until the service stops.

Because of that, we use this line of code to check and see if the Alerter service is running:

If objService.State = “Running” Then

If the service is running, we then call the StopService() method in order to stop the thing. After that we pause for 5 seconds (5,000 milliseconds); that gives the service time to actually stop before we go ahead and disable it. That’s what we do here:

Wscript.Sleep 5000

Note. In the interests of keeping this sample script as short as possible, we’re cheating a little bit. We’re assuming that your service will either be Running or Stopped. In reality, a service can have other states, most notably Paused or Continuing. To make a more bulletproof script, you should probably check for those conditions (and take the appropriate action) as well. But because 99.9% of the time services are either running or stopped, you can usually get by with the sample script we showed you.

Once the service has stopped we then use this line of code to disable it:

errReturnCode = objService.ChangeStartMode(“Disabled”)

As you can see, we’re simply using the ChangeStartMode method to change the value of the StartMode property. We want to disable the service so we pass the parameter “Disabled” to ChangeStartMode. We could also set the StartMode to “Manual” or “Automatic”; that would, well, set the StartMode to either Manual or Automatic.

Remember, once a service has been stopped and disabled it can no longer be started; the only way to restart the service is to re-enable it (by changing the StartMode to Manual or Automatic). But we’ll wait until we’ve talked everyone into disabling everything on their computers before we tell you how to re-enable stuff. After all, we can probably charge double for those scripts. Do you think it would look weird if all of us got the same diamond-studded Rolex?


Discussion is closed.

Feedback usabilla icon