How to extract a specific field from output of tasklist on the windows command line

后端 未结 3 2099
难免孤独
难免孤独 2020-12-14 21:08

I ran the following command on the windows command prompt

C:>tasklist /fi \"Imagename eq BitTorrent.exe\"

The output of which is

3条回答
  •  青春惊慌失措
    2020-12-14 21:31

    the easiest way is with using WMIC:

    c:\>wmic process where caption="BitTorrent.exe" get  ProcessId
    

    EDIT: As the WMIC is not part of home editions of windows:

    for /f "tokens=1,2 delims= " %A in ('tasklist /fi ^"Imagename eq cmd.exe^" ^| find ^"cmd^"') do echo %B
    

    Here is used CMD of the caption.You can change it in the find and tasklist parameters. If this used in batch file you'll need %%B and %%A

提交回复
热议问题