0

I am trying to get process name and process memory usage through a PowerShell command:

Get-Process | Group-Object -Property ProcessName |
    Format-Table Name, @{n='Mem (KB)';e={
        '{0:N0}' -f (($_.Group|Measure-Object WorkingSet -Sum).Sum / 1KB)
    };a='right'} -AutoSize

But when I pipe it with ConvertTo-Json the output is different which gets me details of ClassID, outOfBand, which is not the output I expect. I want the output in exact same way (Name and Mem (KB)) as it displays on screen but in JSON format.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Ashish
  • 1,309
  • 1
  • 11
  • 17
  • Don't pipe `Format-Table` output to `ConvertTo-Json` - use `Select-Object` instead of `Format-Table` – Mathias R. Jessen Apr 10 '16 at 09:48
  • @MathiasR.Jessen But how do I calculate the Memory Size then? When I use Format-Table it calculates but using Select-Object it doesnt calculate the memory used by process – Ashish Apr 10 '16 at 09:53
  • 2
    `Select-Object` accepts calculated properties just like `Format-Table`. Show the code that isn't working if you want qualified help :) – Mathias R. Jessen Apr 10 '16 at 09:58
  • @MathiasR.Jessen Thanks I figured it out :) and answered the question. – Ashish Apr 10 '16 at 10:21

1 Answers1

0

The solution to this problem is by using Select-Object instead of Format-Table

Get-Process | Group-Object -Property ProcessName |
    Select-Object Name, @{n='Mem';
        e={(($_.Group|Measure-Object WorkingSet -Sum).Sum / 1KB)}} |
    ConvertTo-Json
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Ashish
  • 1,309
  • 1
  • 11
  • 17