Skip to content

Instantly share code, notes, and snippets.

@patharanordev
Created November 1, 2021 11:25
Show Gist options
  • Save patharanordev/4f87bdb5100b81274e547cd32e45e59e to your computer and use it in GitHub Desktop.
Save patharanordev/4f87bdb5100b81274e547cd32e45e59e to your computer and use it in GitHub Desktop.
Using PowerShell to stop specific service by process ID from the service name
Function GetServiceByName {
Param (
[string]$ServiceName
)
return Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$ServiceName'" | Select-Object -ExpandProperty ProcessId
}
$nid = GetServiceByName -ServiceName "notepad"
If ($nid -ne 0) {
Write-Host " * Found notepad running (PID:$nid)"
Stop-Process -Id $nid -Force
Write-Host " * notepad stopped"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment