Removing Services

Microsoft® Windows® 2000 Scripting Guide

As your organization changes, you might decide to remove certain services from certain computers. In-house and third-party services can be removed by using WMI, while operating system services can be removed by using Sysocmgr.exe.

When preparing to remove services, keep the following information in mind:

  • Services must be stopped before you remove them.

    If the service is running when you issue the delete command, the service is marked for deletion, but it continues to run until it stops and all open handles are closed. If the service is never stopped, that service will never be deleted.

  • Removing a service does not remove the service's executable file.

    Removing a service by using WMI deletes the related registry entries under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. As a result, the service is no longer installed and is not available through the Services snap-in. However, WMI does not delete the executable file, meaning you could easily reinstall the service. To delete the executable file, you must retrieve the path name and then delete the file.

  • Removing a base Windows 2000 service (for example, DHCP) by using WMI deletes the registry entries for that service but does not remove the shortcut from the Administrative Tools menu or remove the service from the Windows Components Wizard. This can confuse anyone trying to determine how the computer has been configured.

    For example, if you remove the DHCP service by using a WMI script, the DHCP service is no longer listed in the Services snap-in. However, a nonfunctioning shortcut to the DHCP console remains in the Administrative Tools menu, and if you start the Windows Component Wizard, it indicates that the DHCP service is installed.

    Because of this, you should always use Sysocmgr.exe to programmatically remove Windows 2000 services.

Scripting Steps

Listing 15.23 contains a script that removes a service named DbService. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Service class. To restrict data retrieval to a single service, a Where clause is included to limit data retrieval only to the service named DbService. This ensures that the correct service is retrieved because service names must be unique.

  4. For the single service in the collection, use the StopService method to stop the service.

  5. Use the Delete method to delete the service.

Listing 15.23 Removing a Service


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Service WHERE Name = 'DbService'")
For Each objService in colListOfServices