How to get process id by its service name with a script to variable

80,714

Solution 1

tasklist is just returning text, not actual objects that have properties you can access. You can use WMI to get this information instead:

$id = Get-WmiObject -Class Win32_Service -Filter "Name LIKE 'WinDefend'" | 
      Select-Object -ExpandProperty ProcessId

$process = Get-Process -Id $id

Update for PowerShell Core

In version 6, Windows PowerShell started towards cross platform support with PowerShell Core based on .NET Core. This led to many changes in cmdlets that were Windows-centric and some being left out completely. WMI is a Windows only technology, so its cmdlets (e.g. Get-WmiObject) were not ported over. However, its features are available via CIM cmdlets (e.g. Get-CimInstance) here is a version that will work on PowerShell 6+:

$id = Get-CimInstance -Class Win32_Service -Filter "Name LIKE 'WinDefend'" | 
      Select-Object -ExpandProperty ProcessId

$process = Get-Process -Id $id

Solution 2

$p=Tasklist /svc /fi "SERVICES eq windefend" /fo csv | convertfrom-csv
$p.PID

Solution 3

Annoying as this is, it requires you to set a unique title for your script if you want the pid for the current process. Then search for that unique title within the list of processes. Thankfully, the Title command allows you to do just that. Also see MagicAndi's response...

Here is my batch file solution:

@ECHO OFF
:SetVars
    SET _Thread=%1
    title=ExecBatch_%_Thread%
    Set /A "_iPID=0"
:Main
    CALL :getPID _iPID %_Thread%
    ...
EXIT /b

::----------------
::---- GetPID ---- 
::----------------
:getPID 
    setlocal   
        set _getPIDcmd=tasklist /v /fo csv 
        for /f "tokens=2 delims=," %%i in ('%_getPIDcmd% ^| findstr /i "ExecBatch_%2"') do (
            echo %%~i
            set _pid=%%~i
        )
    endlocal & Set %~1=%_pid%
exit /b

BTW, I've had the 'pleasure' of doing this time and time again over the years, via API, or batch, or ps. Pick your poison - on a Windows platform it's all the same.

I found an even better way via powershell: $pid returns the current process' process id.

Solution 4

# Enter servicename. (instead of 'netman')
$service = Get-CimInstance -class win32_service | Where-Object name -eq 'netman' | select name, processid
$process = Get-Process | Where-Object ID -EQ $service.processid
Clear-Host
Write-Host '********* ServiceName, PID and ProcessName ******'
Write-Host 'ServiceName:' $service.name 
Write-Host 'ID:' $process.Id 
Write-Host 'ProcessName:' $process.Name

Thanks,

Share:
80,714

Related videos on Youtube

E235
Author by

E235

Updated on January 02, 2022

Comments

  • E235
    E235 over 2 years

    I have service named WinDefend and it runs on process svchost.exe
    There other many svchost.exe processes and I need to find a way to get its ID.
    when I run tasklist /svc I can see: enter image description here

    I am not sure how can I get it.
    I found this command but when I tried the select "PID" it gave me empty column. enter image description here

    I need to get the PID of the process to variable.

  • Nick
    Nick over 5 years
    While this answer may solve the question it would be much more useful if you explained how it works
  • csgroen
    csgroen almost 3 years
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
  • Raul Chiarella
    Raul Chiarella over 2 years
    I am sorry for the very dumb question but where do i execute this?
  • Mike Zboray
    Mike Zboray over 2 years
    @RaulChiarella In powershell? :) I'm not sure quite what you mean, but with powershell core being the standard now I've made an update that should work with that version.

Related