StopService

Places a service in the stopped state.

After you have determined which services can be stopped or paused, you can use the StopService and PauseService methods to stop and pause services. The decision to stop a service rather than pause it, or vice versa, depends on several factors, including the following:

The scripting code required to stop a service is almost identical to the code required to pause the service.

If you attempt to stop a service which has dependent services running, the StopService method fails with a return value of 3. The dependent services must be stopped first.

If you stop a service, then immediately check the Win32_Service.State property, as the value may still show the service as running.

The Set-RemoteService PowerShell sample Sets service state for remote machines.

The Stop a Service and Its Dependents VBScript sample stops a service and all dependent services.

The following VBScript code sample demonstrates how to shut down a service.

The following Perl code sample demonstrates how to shut down a service.

The following VBScript code example shows that you cannot stop the NetDDE service until the dependent services have been stopped. To run the script, ensure that the NetDDE service and its dependent services are running by using the Services.msc MMC snap-in or the Net Start command.

The Win32_DependentService class allows you to locate service dependencies through an Associators Of query.

Example

Do not run below example code just to see what happens next. Many methods seriously affect your system. Always make sure you actually understand what the method and the code do.

$query = 'Select * From Win32_Service where Name="Spooler"'

Invoke-CimMethod -Query $query -MethodName StopService |
  Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        1        {'Not Supported'}
        2        {'Access Denied'}
        3        {'Dependent Services Running'}
        4        {'Invalid Service Control'}
        5        {'Service Cannot Accept Control'}
        6        {'Service Not Active'}
        7        {'Service Request Timeout'}
        8        {'Unknown Failure'}
        9        {'Path Not Found'}
        10       {'Service Already Running'}
        11       {'Service Database Locked'}
        12       {'Service Dependency Deleted'}
        13       {'Service Dependency Failure'}
        14       {'Service Disabled'}
        15       {'Service Logon Failed'}
        16       {'Service Marked For Deletion'}
        17       {'Service No Thread'}
        18       {'Status Circular Dependency'}
        19       {'Status Duplicate Name'}
        20       {'Status Invalid Name'}
        21       {'Status Invalid Parameter'}
        22       {'Status Invalid Service Account'}
        23       {'Status Service Exists'}
        24       {'Service Already Paused'}
        default  {"Unknown Error $_"}
    }
}

To run this method on one or more remote systems, use New-CimSession:

$session = New-CimSession -ComputerName $ComputerName -Credential $Credential

$query = 'Select * From Win32_Service where Name="Spooler"'

Invoke-CimMethod -Query $query -MethodName StopService -CimSession $session |
  Add-Member -MemberType ScriptProperty -Name ReturnValueFriendly -Passthru -Value {
  switch ([int]$this.ReturnValue)
  {
        0        {'Success'}
        1        {'Not Supported'}
        2        {'Access Denied'}
        3        {'Dependent Services Running'}
        4        {'Invalid Service Control'}
        5        {'Service Cannot Accept Control'}
        6        {'Service Not Active'}
        7        {'Service Request Timeout'}
        8        {'Unknown Failure'}
        9        {'Path Not Found'}
        10       {'Service Already Running'}
        11       {'Service Database Locked'}
        12       {'Service Dependency Deleted'}
        13       {'Service Dependency Failure'}
        14       {'Service Disabled'}
        15       {'Service Logon Failed'}
        16       {'Service Marked For Deletion'}
        17       {'Service No Thread'}
        18       {'Status Circular Dependency'}
        19       {'Status Duplicate Name'}
        20       {'Status Invalid Name'}
        21       {'Status Invalid Parameter'}
        22       {'Status Invalid Service Account'}
        23       {'Status Service Exists'}
        24       {'Service Already Paused'}
        default  {"Unknown Error $_"}
    }
}


Remove-CimSession -CimSession $session

Learn more about Invoke-CimMethod and invoking WMI methods.

Syntax

uint32 StopService();

Parameters

The method takes no arguments.

Return Value

Returns a value of type UInt32. Return values:

$returnValues = @{
    0    = 'Success'
    1    = 'Not Supported'
    2    = 'Access Denied'
    3    = 'Dependent Services Running'
    4    = 'Invalid Service Control'
    5    = 'Service Cannot Accept Control'
    6    = 'Service Not Active'
    7    = 'Service Request Timeout'
    8    = 'Unknown Failure'
    9    = 'Path Not Found'
    10   = 'Service Already Running'
    11   = 'Service Database Locked'
    12   = 'Service Dependency Deleted'
    13   = 'Service Dependency Failure'
    14   = 'Service Disabled'
    15   = 'Service Logon Failed'
    16   = 'Service Marked For Deletion'
    17   = 'Service No Thread'
    18   = 'Status Circular Dependency'
    19   = 'Status Duplicate Name'
    20   = 'Status Invalid Name'
    21   = 'Status Invalid Parameter'
    22   = 'Status Invalid Service Account'
    23   = 'Status Service Exists'
    24   = 'Service Already Paused'
}

See Also

Additional methods implemented by Win32_Service:

Change()

Change() modifies a service.

ChangeStartMode()

ChangeStartMode() modifies the start mode of a service.

Create()

Create()

Delete()

Delete() deletes an existing service.

GetSecurityDescriptor()

GetSecurityDescriptor() returns the security descriptor that controls access to the service.

InterrogateService()

InterrogateService() requests that a service update its state to the service manager.

PauseService()

PauseService() attempts to place a service in the paused state.

ResumeService()

ResumeService() attempts to place a service in the resumed state.

SetSecurityDescriptor()

SetSecurityDescriptor() writes an updated version of the security descriptor that controls access to the service.

StartService()

StartService() attempts to place a service into the startup state.

UserControlService()

UserControlService() attempts to send a user-defined control code to a service.

Requirements

To use Win32_Service, the following requirements apply:

PowerShell

Get-CimInstance was introduced with PowerShell Version 3.0, which in turn was introduced on clients with Windows 8 and on servers with Windows Server 2012.

If necessary, update Windows PowerShell to Windows PowerShell 5.1, or install PowerShell 7 side-by-side.

Operating System

Win32_Service was introduced on clients with Windows Vista and on servers with Windows Server 2008.

Namespace

Win32_Service lives in the Namespace Root/CIMV2. This is the default namespace. There is no need to use the -Namespace parameter in Get-CimInstance.

Implementation

Win32_Service is implemented in CIMWin32.dll and defined in CIMWin32.mof. Both files are located in the folder C:\Windows\system32\wbem:

explorer $env:windir\system32\wbem
notepad $env:windir\system32\wbem\CIMWin32.mof