How to calculate and use memory of several instances of one process using powershell?

I have the following result while running powershell command,

PS C:\> Get-Process svchost Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 546 34 18528 14884 136 49.76 260 svchost 357 14 4856 4396 47 18.05 600 svchost 314 17 6088 5388 42 12.62 676 svchost 329 17 10044 8780 50 12.98 764 svchost 1515 49 36104 38980 454 232.04 812 svchost 301 33 9736 6428 54 2.90 832 svchost 328 26 8844 9744 52 4.32 856 svchost 247 18 8144 9912 77 37.50 904 svchost 46 5 1504 968 14 0.02 1512 svchost 278 15 4048 5660 43 3.88 2148 svchost 98 14 2536 2460 35 0.66 2504 svchost 

Here I am trying to calculate the total memory PM(K) process (s). i''s following a line in my ps1 script file

 get-process svchost | foreach {$mem=("{0:N2}MB " -f ($_.pm/1mb))} 

It displays the result in the following format

 17.58MB 4.79MB 6.05MB 9.99MB 35.29MB 9.56MB 8.64MB 7.95MB 1.47MB 3.95MB 2.48MB 

but I need the total size as one value, for example 107.75MB

How to calculate the total size of used svchost process svchost ?

thanks

+7
windows powershell windows-server-2008-r2
source share
2 answers

You can use the Measure-Object cmdlet

 $measure = Get-Process svchost | Measure-Object PM -Sum $mem = ("{0:N2}MB " -f ($measure.sum / 1mb)) 
+10
source share

In addition, you can calculate the total size of the entire collection using the syntax + =

 $mem = 0 Get-Process svchost | %{$mem += $_.pm} "{0:N2}MB " -f ($mem/1mb) 
+1
source share

All Articles