7.8. Searching Services


You want to find one or more services that match certain criteria.


Using a graphical user interface

The Services snap-in does not allow you to search services.

Using a command-line interface:

With the sc.exe command you can query against service type (type= <TypeName>), state (state= <State>), and group (group= <GroupName>).


For more on service groups, see Recipe 7.11.

Here are two examples:

> sc <ServerName> query state= active
> sc <ServerName> query type= driver

The psservice.exe command is very similar; you can query against service type (type= <TypeName>), state (state= <State>), and group (group= <GroupName>).

For example:

> psservice \\<ServerName> query -s active
> psservice \\<ServerName> query -t driver

Using VBScript

' This code queries services based on the specified 
' property name and value
strPropName  = "State"
strPropValue = "'Running'"     ' e.g., 
strComputer  = "<ServerName>" ' e.g., fs-rtp01 (use . for local server) ' ------ END CONFIGURATION --------- set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set colServices = objWMI.ExecQuery _ ("Select * from Win32_Service Where " & _ strPropName & " = " & strPropValue) for each objService in colServices Wscript.Echo objService.DisplayName next ' List of Win32_Service properties: ' boolean AcceptPause ' boolean AcceptStop ' string Caption ' uint32 CheckPoint; ' string CreationClassName ' string Description ' boolean DesktopInteract ' string ...

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.