Check if a Windows service exists and delete in PowerShell

Adrian Russell picture Adrian Russell · Feb 11, 2011 · Viewed 201.1k times · Source

I am currently writing a deployment script that installs a number of Windows services.

The services names are versioned, so I want to delete the prior Windows service version as part of the installs of the new service.

How can I best do this in PowerShell?

Answer

ravikanth picture ravikanth · Feb 11, 2011

You can use WMI or other tools for this since there is no Remove-Service cmdlet until Powershell 6.0 (See Remove-Service doc)

For example:

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

Or with the sc.exe tool:

sc.exe delete ServiceName

Finally, if you do have access to PowerShell 6.0:

Remove-Service -Name ServiceName