September 13, 2017

PowerShell - get Process ID of running service

How to get a Process ID of running service using PowerShell?

Get-WmiObject -Class win32_service | where {$_.Name -eq "Service Name"}

Example below will show a Process ID of “Task Scheduler” service:
Get-WmiObject -Class win32_service | where {$_.Name -eq "Schedule"}
ExitCode  : 0
Name      : Schedule
ProcessId : 11900
StartMode : Auto
State     : Running
Status    : OK

Or use more convenient way through Caption property:
Get-WmiObject -Class win32_service | where {$_.Caption -eq "Task Scheduler"}

Want to kill Task Scheduler process?
Note that ID (11900) is taken from example above. This example sometime can be useful with services, which are in Stop Pending state.
Stop-Process 11900 -Force
Or even simpler without identifying Process ID, single string syntax:
Stop-Process (Get-WmiObject -Class win32_service | where {$_.Name -eq "Schedule"}).ProcessID -Force

List of all running services. Example will select only Name, Caption and ID properties:
Get-WmiObject -Class win32_service | where {$_.State -eq "Running"} | select Name,Caption,ProcessID

No comments:

Post a Comment