Check If A Windows Service Exists And Delete In PowerShell


Answer :

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 

There's no harm in using the right tool for the job, I find running (from Powershell)

sc.exe \\server delete "MyService"  

the most reliable method that does not have many dependencies.


If you just want to check service existence:

if (Get-Service "My Service" -ErrorAction SilentlyContinue) {     "service exists" } 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?