How to delete a Windows service in PowerShell

By FoxLearn 11/5/2024 7:03:27 AM   6
To delete a Windows service using PowerShell, you'll need to stop the service and then remove it from the system.

Press Win + X and select Windows PowerShell (Admin) from the menu.

powershell

First, stop the service before you delete it.

You can run the following command

Stop-Service -Name "ServiceName"

If the service is already stopped or if you don't need to stop it first, you can skip this step.

To delete the service, use the Remove-Service command.

Remove-Service -Name "ServiceName"

How to remove a windows service using the display name?

To remove a service named 'TestService'. I use the `Get-Service` cmdlet to retrieve the service object by its display name. The object is then passed through the pipeline (`|`) to the `Remove-Service` cmdlet, which deletes the service.

Get-Service -DisplayName "Test Service" | Remove-Service

To check if a Windows service exists in PowerShell, you can use the `Get-Service` cmdlet to query the service by its name.

Get-Service "ServiceName"

If it’s deleted, you should get an error saying the service cannot be found.

Since there is no `Remove-Service` cmdlet in PowerShell versions prior to 6.0, you can use WMI (Windows Management Instrumentation) or other tools like `sc.exe` to remove a service. In PowerShell 6.0 and later, `Remove-Service` is available for directly removing services.

$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()

Or use sc.exe

sc.exe delete ServiceName

Make sure you use the service name, not the display name. You can find the service name by going to services.msc and checking the properties of the service.

Deleting a service requires administrative privileges, which is why you need to run PowerShell as an administrator.