Recipe 7.3. Removing a ServiceProblemYou want to remove a service. When you uninstall an application that previously installed a service, the service will automatically be removed. However, if you manually installed the service as described in Recipe 7.2, you'll need to manually remove it to uninstall it. SolutionBefore you can remove a service, you need to make sure the service is not running. See Recipe 7.1 for more on stopping a service. Using a graphical user interface
Using a command-line interface:The following commands stop a service and remove it: > sc <ServiceName> stop > instsrv <ServiceName> remove Using VBScript' This code removes a service. ' ------ SCRIPT CONFIGURATION ------ strComputer = "<ServerName>" ' e.g., fs-rtp01 (use . for local server) strSvcName = "<ServiceName>" ' e.g., MyMonitor boolStopService = TRUE ' e.g., TRUE to attempt to stop the service ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'") if boolStopService = TRUE then intRC = objService.StopService WScript.Sleep 5000 ' Give the service 5 seconds to stop if intRC > 0 then WScript.Echo "Error stopping service: " & intRC WScript.Quit else WScript.Echo "Successfully stopped service" end if end if intRC = objService.Delete if intRC > 0 then WScript.Echo "Error deleting service: " & intRC else WScript.Echo "Successfully deleted service" end if DiscussionAnytime you uninstall an application, the uninstall program should automatically remove the services that were installed with the application. If you find that it doesn't, you should complain loudly to the company that made that software. If, however, you manually created your own service, the only way to remove it when you no longer want it is to manually delete it. All three solutions use a slightly different method for removing a service, but they are all essentially performing the same task. Removing a service consists of removing the associated entries from the registry (HKLM\SYSTEM\CurrentControlSet\Services\<ServiceName>) and deregistering the service with the SCM. See AlsoRecipe 7.2 and MSDN: Delete Method of the Win32_Service Class |