As part of creating some test infrastructure for a work project the other day, I was looking for a good way to find any existing / leftover processes, so I could ensure things were in a clean state at the start of each test run. On Linux this is pretty easy, using ps, but I couldn’t find a good way to do it on Windows.
There’s the tasklist command, which will output the following:
And for most cases, that’s enough. But I was looking for a particular Java process and unfortunately I couldn’t tell which of the java.exe processes were the ones I was interested in and which were for something else – as tasklist doesn’t provide enough information (i.e. the launch parameters).
After a fair bit of Googling (which I’m hoping to save someone), I came accross a pretty cool command line interface called WMIC (Intro & Examples) that’s immensely powerful for gathering information from Windows-based systems (and is installed by default since Windows 2000).
It seems to do a whole load of stuff, but I was particularly interested in the process side of things and found that I could simply do:
Now, this spits out info for all running processes (I’ve greatly trimmed down the result above), but I then found you can also pass in WHERE commands to restrict things to exactly what you want. E.g.
Which I could then easily parse to get the info I wanted.
Note: In the above examples, I’ve restricted the fields that are returned. If you don’t restrict the GET, the full list of things that is returned is: