Created
November 1, 2021 11:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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