WMI Tasks: Services

WMI tasks for services obtain information about services, including dependent or antecedent services. For other examples, see the TechNet ScriptCenter at https://www.microsoft.com/technet.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...determine which services are running and which ones are not? Use the Win32_Service class to check the state of all of the services. The state property lets you know if a service is stopped or running.
VB
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service",,48) 
For Each objItem in colItems 
    Wscript.Echo "Service Name: " & objItem.Name & VBNewLine & "State: " & objItem.State
Next
PowerShell
Get-WmiObject -Class win32_service -computer "." -Namespace "root\cimv2" | format-list Name, State
...stop Power Users from starting certain services?

Use the Win32_Service class and the ChangeStartMode method to set the StartMode property to Disabled. Disabled services cannot be started, and, by default, Power Users cannot change the start mode of a service.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where StartMode = 'Manual'")
For Each objService in colServiceList
    errReturnCode = objService.Change( , , , , "Disabled")
    WScript.Echo "Changed manual service to disabled: " & objService.Name   
Next
PowerShell
Get-WmiObject -Class win32_service -computer "." -Namespace "root\cimv2" | where {$_.startMode -eq "Manual"} | `
    foreach-object { [void]$_.changeStartMode('Disabled') }
...start and stop services?

Use the Win32_Service class and the StopService and StartService methods.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name ='Alerter'")
For Each objService in colListOfServices
    objService.StartService()
    Wscript.Echo "Started Alerter service"
Next
PowerShell
Get-WmiObject -Class win32_service -computer "." -Namespace "root\cimv2" | where {$_.Name -eq "Alerter"} | `
    foreach-object { [void]$_.StartService() }
...change service account passwords using a script?

Use the Win32_Service class and the Change method.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service")
For Each objservice in colServiceList
    If objService.StartName = ".\netsvc" Then
        errReturn = objService.Change( , , , , , , , "password")  
    End If 
Next
..determine which services I can stop?

Use the Win32_Service class, and check the value of the AcceptStop property.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery ("Select * from Win32_Service Where AcceptStop = True")
For Each objService in colServices
    Wscript.Echo objService.DisplayName 
Next
PowerShell
Get-WmiObject -Class win32_service -computer "." -Namespace "root\cimv2" | where {$_.AcceptStop -eq "True"} | `
     format-list DisplayName
...find the services that must be running before I can start the DHCP service?

Query for ASSOCIATORS OF the Win32_Service class named "DHCP" that are in the Win32_DependentService class and have "Dependent" in the Role property. Role means the role of the DHCP service: in this case, it is dependent on the other services that are being started.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators Of " _ 
    & "{Win32_Service.Name='dhcp'} Where " _
    & "AssocClass=Win32_DependentService " _
    & "Role=Dependent") 
For Each objService in colServiceList
Wscript.Echo objService.DisplayName 
Next
PowerShell
$query = "Associators Of {Win32_Service.Name='dhcp'} Where AssocClass=Win32_DependentService Role=Dependent"
Get-WmiObject -Query $query -Namespace "root\cimv2" | format-list DisplayName
...find the services that require the WMI service (Winmgmt) service to be running before they can start?

Query for ASSOCIATORS OF the Win32_Service class named "DHCP" that are in the Win32_DependentService class and have "Antecendent" in the Role property. Role means the role of the rasman service: in this case, it is antecedent to must be started before the dependent services.

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\ & strComputer & "\root\cimv2")
Set colServiceList = _
    objWMIService.ExecQuery("Associators of " _
    & "{Win32_Service.Name='winmgmt'} Where " _
    & "AssocClass=Win32_DependentService " _
    & "Role=Antecedent" )
For Each objService in colServiceList
Wscript.Echo "Name: " & objService.Name & VBTab & "Display Name: " & objService.DisplayName 
Next
PowerShell
$query = "Associators of {Win32_Service.Name='winmgmt'} Where AssocClass=Win32_DependentService Role=Antecedent"
Get-WmiObject -Query $query -Namespace "root\cimv2" | format-list Name, DisplayName

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter

`