-1

With lots of research, I've got to this point:

tasklist /fi "pid eq 13584" /fo CSV

Output:

"Image Name","PID","Session Name","Session#","Mem Usage"
"php.exe","13584","Console","1","25 660 K"

Still an ugly mess. I'm trying to get output such as:

25660000

That is, no CSV or other "formatting"/unwanted data. No "formatted" amount of memory. Just raw bytes.

How is it done?

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Dec 08 '21 at 13:53
  • According to the `tasklist` documentation it's fixed to KB. You'd need to parse the result and convert it to bytes with another command/function. – spikey_richie Dec 08 '21 at 13:56
  • 1
    Possibly use PowerShell instead. – Daniel B Dec 08 '21 at 14:01
  • There is nothing to clarify. The question couldn't be more clear. – Dezmon G. Dec 08 '21 at 14:11
  • @DanielB Not possible. cmd.exe. – Dezmon G. Dec 08 '21 at 14:11
  • 1
    @DezmonG. Powershell is a good thing to use, but you can run Powershell commands from `cmd`. Initially I wrote my answer from the Powershell perspective but have edited it to show how to run a useful Powershell command from `cmd`. Powershell should be available on any system running Windows 7 SP1 or newer. – Mokubai Dec 08 '21 at 14:32
  • 2
    There’s always something to clarify. For example, what are you going to use the data for? In the answer’s comments you mention PHP. Is that related? The bigger picture _never_ hurts and is _almost always_ helpful in finding a good solution to a problem. See also [_What is the XY problem?_](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Daniel B Dec 08 '21 at 16:38

1 Answers1

1

cmd is not the greatest way to do this in a modern system. Powershell is far more versatile and, once you get used to the syntax and symantics, far more powerful.

For example, to list all the processes in a system:

PS C:\Users\user> Get-Process

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    325      20    10328      28056       0.28  17868   2 ApplicationFrameHost
    208      15     7308      12952      22.95  12092   0 audiodg
    476      33    26572      15592     106.06   8852   2 BorderlessGaming
    155      11     1792       7600              4132   0 BtwRSupportService
     53       4      672       2980              5096   0 cdarbsvc_v1.0.0_x64
    142       7     1616       9092       0.05  17728   2 CompPkgSrv
... etc

From there you can list a specific PID:

PS C:\Users\user> Get-Process -PID 12092

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    208      12     7308      12952      31.48  12092   0 audiodg

PM = Private Memory
WS = Working Set

To get only the memory:

PS C:\Users\user> (Get-Process -PID 12092).WorkingSet
13217792
PS C:\Users\user> (Get-Process -PID 12092).PrivateMemorySize
7413760

If you have powershell available on your system you can use it to run the command and return it to cmd:

cmd> powershell.exe -command "(Get-Process -PID 12092).PrivateMemorySize"
7598080

There are some suggestions that powershell may not properly output when run from certain environments and needs you to use Write-Output as well as -InputFormat none so try

powershell.exe -InputFormat none -command "Write-Output((Get-Process -PID 12092).PrivateMemorySize)"

make sure that you are properly escaping quotes and so on in your php script.

Mokubai
  • 89,133
  • 25
  • 207
  • 233
  • While your command works when I run it interactively in a cmd.exe, it completely messes up everything if I run it in the actual context, which is from a PHP CLI script which was launched with cmd.exe. It visually morphs the entire box to use a different font, and doesn't return any data. This is the reason why I've avoided PowerShell -- it never works correctly from PHP CLI scripts. – Dezmon G. Dec 08 '21 at 15:17
  • You have no idea how many hours of my life I've wasted trying to "force" my PHP CLI scripts to run from/as PowerShell. It just doesn't work. There are numerous bizarre problems. It seems like cmd.exe is still the expected command prompt/terminal on Windows. And isn't PowerShell itself also legacy now with the upcoming Windows Terminal? – Dezmon G. Dec 08 '21 at 15:18
  • @Dezmon cmd and powershell are *shells*, like bash or zsh on Unix. OTOH Windows terminal is a *terminal* like conhost.exe or xterm. A shell must be connected to a terminal to show the outputs and read inputs. They have nothing in common. Windows terminal still shows a powershell prompt by default on newer Windows. On older Windows conhost.exe will run when you run cmd or powershell – phuclv Dec 08 '21 at 15:30
  • @phuclv Well, the bottom line is that PowerShell stuff never works for me unless run interactively (manually). – Dezmon G. Dec 08 '21 at 15:36
  • 1
    @DezmonG. it might be worth asking a question about that particular problem if you have not done so already. It would be good to see what you mean by "morphs the entire box". Having a [quick google](https://stackoverflow.com/questions/5317315/executing-a-powershell-script-from-php) suggests that `-InputFormat none` might help as well. I've edited my answer with another possible command. – Mokubai Dec 08 '21 at 15:48
  • @Mokubai Sadly, even the updated command still changes the whole look of the cmd.exe box and doesn't return any output. I don't know what to say. PowerShell has been impossible to use with PHP CLI since forever, and I've repeatedly tried it. – Dezmon G. Dec 08 '21 at 16:56
  • 1
    @DezmonG. Your question is not about PHP CLI though and as it is my answer appears to be a solution to your specific question. I'm interested in finding a solution to your specific PHP problem, but would need more information on how to replicate your issue such as what PHP you are using, what commands you are running and where and what outputs you get. As mentioned it would be worth asking as a new distinct question. It might be worth you registering your account by linking so that you don't lose access if you clear cookies. https://superuser.com/help/creating-accounts – Mokubai Dec 08 '21 at 17:22