I am trying to figure out a tasklist
command that gives the Description
aswell as shown in the Taskmangaer UI?I am trying to run it from python,if it is not posible is there an equivalent python command to get list of all tasks with description?
tasklist /?
That's a bit trickier than you might imagine and you really need a good reason to go through all the trouble to justify it. First of all, Task Manager UI doesn't get its information from
tasklist.exe
, although you can get pretty close with:However, to get to the
Description
field (and a lot others from the Task Manager UI) you'll have to pull the data from WMI at the very least. To make matters worse,WMIC
on Windows 7 has a bug when exporting to CSV making the whole thing even more complicated as for maximum portability we need to use thelist
format and parse it ourselves:If you don't need all these fields, you can always restrict
wmic
to get you the fields you want (i.e.wmi_out = subprocess.check_output(["wmic", "process", "get", "ProcessId,ExecutablePath,Description", "/format:list"])
to get onlyDescription
perProcessId
).But don't think your troubles are over - we just started. While we now have the
Description
field (and a few others to boot), you'll notice that for processes that do not announce their description (most of them, Windows programmers be lazy apparently) or services without a description - the description value just contains the executable name i.e. if you're running plain old Notepad, while Task Manager UI will show youNotepad
as Description, its dictionary entry will havenotepad.exe
- that is because Task Manager UI uses a completely different approach to task list and gets the description directly from the process executable.So you actually need an additional step to retrieve the executable description directly from its resources table, which is probably the 'easiest' to do by invoking the Win32 API to get to the description, so you need to install the pyWin32 module first:
Voilà! Descriptions are now populated (where available, or at least hold the executable name), but since we had to use Win32 API to get to the descriptions, we might as well get the tasks list through it - it's faster and more concise:
This will only get ProcessId, ExecutablePath and Description but you can further explore Win32 API if you need more fields.
Again, I don't see of what value the
Description
field is to go through all this trouble but if you really, really want it - this is how to get it.