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:
The following VBScript code sample describes how to delete a service.
The following Perl code sample describes how to delete a service.
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 Delete |
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 Delete -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 Delete();
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()
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.
StopService()
StopService() places a service in the stopped 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