user4182277
user4182277

Reputation: 950

Mathematical operations in pipeline

I use following code to get chrome total memory usage in megabytes

$usage = Get-Process | Where-Object {$_.Name -eq "chrome"} | Measure-Object -Sum -Property PrivateMemorySize
"Total: " + $usage.Sum / 1e+6;

How can I do same thing in one line? I've figured out how to access properties in pipeline, but I don't know how to divide it by 1e+6.

Get-Process | Where-Object {$_.Name -eq "chrome"} | Measure-Object -Sum -Property PrivateMemorySize | select Sum

Upvotes: 2

Views: 742

Answers (3)

mklement0
mklement0

Reputation: 437988

An alternative solution that uses true binary MB units (mebibytes, [math]::Pow(2, 10*2), 1,048,576 bytes) rather than decimal ones ([math]::Pow(10,6) / 1e6, 1,000,000 bytes)

"Total: $(((Get-Process 'chrome').PrivateMemorySize | measure -Sum).Sum / 1mb)"

(Replace 1mb with 1e6 if you really want to use decimal units.
Note, though, that in terms of RAM - as opposed to mass storage - binary units are still prevalent)

Upvotes: 0

James Woolfenden
James Woolfenden

Reputation: 6661

To get exactly what you asked for, use:

((Get-Process | Where-Object {$_.Name -eq 'chrome'} | Measure-Object -Sum -Property PrivateMemorySize|% {$_.Sum}) /1e+6)|% {"Total: $_"}

Upvotes: 2

Esperento57
Esperento57

Reputation: 17472

just simply like this :)

"Total: {0} " -f ((gps -Name "chrome" | Measure -Sum PrivateMemorySize).Sum/ 1e+6);

Upvotes: 2

Related Questions