How to Delete Services Using PowerShell
There will be multiple situations where we need to clean the services up. This problem was caused by uninstalling software leaving its driver entries or service in the registry.
Windows will try to load them at every boot, fails, and log the error to the System Event log at every startup.
This article tells us how to delete an orphaned service in Windows 10 and earlier using the command-line and PowerShell.
Deleting a Service Using the Command Line in PowerShell
The sc.exe
legacy command-line tool in Windows can create, read, edit, or delete Windows Services. To remove services in Windows, use the following command-line syntax from admin Command Prompt.
Please ensure that you run in an elevated command prompt with administrator privileges.
sc delete service_name
The value service_name
refers to the short name of the service instead of its display name. Open the Services MMC and double-click the service you want to check to find the short name.
Deleting a Service Using PowerShell
You can also run the sc.exe command in PowerShell.
We need to specify the extension sc.exe
when running it in PowerShell because the command SC
in PowerShell will be interpreted as Set-Content
, a built-in cmdlet in PowerShell.
sc.exe delete service_name
To get the name of the service to be removed, run Get-Service
on the machine known to have the orphaned service. We will use the column marked Name
for the service name in the PowerShell script.
Get-Service
We can use the following native commands from the PowerShell administrator window to delete a service.
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()
It’s even easier if we have PowerShell 6.0 installed. In PowerShell 6 and higher, you can use this syntax to remove a service:
Remove-Service -Name ServiceName
Running the Remove-Service
command in older versions of PowerShell below 6.0 shows the error:
The Remove-Service
cmdlet is not recognized as the name of a cmdlet, function, script file, or operable program.
Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.
LinkedInRelated Article - PowerShell Command
- How to Perform awk Commands in PowerShell
- How to Jump Around to Certain Spots in PowerShell Script
- Write-Verbose vs Write-Host in PowerShell
- PowerShell Command Equivalent to Linux ls
- How to Measure Runtime of a Script Using PowerShell