Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Last active April 6, 2018 09:29
Show Gist options
  • Select an option

  • Save JohnLBevan/319c99c36a2753b3e95171f5d6daf99f to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/319c99c36a2753b3e95171f5d6daf99f to your computer and use it in GitHub Desktop.
Delete a Windows service (works fine with service names containing spaces too; uses the service's short name; i.e. Service Name rather than Display Name ). See https://stackoverflow.com/a/76127/361842 for more info.
function Remove-Service {
[CmdletBinding()]
Param (
[Parameter()]
[string]$Name
)
Process {
#could be improved by implementing everything the full suite of parameters that Start/Stop-Service have; for now just fulfils the most basic requirement
Stop-Service -Name $Name -Force -ErrorAction SilentlyContinue
sc.exe delete $Name
}
}
@JohnLBevan

Copy link
Copy Markdown
Author

Can also be handled using WMI; I may change it in a later implentation to allow this to handle more advanced scenarios.

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

There's also a much better implementation coming soon to a PowerShell near you: PowerShell/PowerShell#4858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment