If I run the following command in PowerShell, it will return to me an object with the sum of the WorkingSet property:
PS > get-process chrome | measure-object WorkingSet -sum
Count : 36
Average :
Sum : 2377129984
Maximum :
Minimum :
Property : WorkingSet
If I want to return just the Sum
value, the only way I've found to do it is:
PS > $a = get-process chrome | measure-object WorkingSet -sum
PS > $a.Sum
2359980032
Is there a way to pipe the original command to some other command so that I return the integer in a single step? Something like this:
PS > get-process chrome | measure-object WorkingSet -sum | {some other command}
Yes, use Select-Object:
Get-Process chrome | Measure-Object WorkingSet -sum | Select-Object -expand Sum
Select-Object is used when one needs only some properties from the original object. It creates a new object and copies the desired properties to the new one.
Get-Process | Select-Object -property ProcessName, Handles
In case you need to extract the value (from one property), you use parameter -expandProperty
instead of -property
:
Get-Process | Select-Object -expand ProcessName
Note that you can compute the returned value:
get-process | Select-Object -property @{Name='descr'; Expression={"{0} - {1}" -f $_.ProcessName, $_.Handles }}
Just use parentheses to enclose the piped command:
(get-process chrome | measure-object WorkingSet -sum).sum
As a rule, PowerShell treats anything enclosed in parentheses as an object that you can reference by properties/methods.
Another way:
(get-process chrome | measure-object WorkingSet -sum).sum
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With