ecthelion84
ecthelion84

Reputation: 356

Getting a specific part from a PowerShell command output

I am calling the following PowerShell command and assigning the output to a variable:

$my_variable = (Get-Counter -Counter "\Process(process1)\Working Set - Private") | Out-String

The output is:

> Timestamp                 CounterSamples
> ---------                 -------------- 
> 4/7/2015 3:54:00 PM       \\smachine101\process(process1)\working set - private :
>                           15298560

All I need is '15298560' from the output.

Can you recommend a way to accomplish this?

Upvotes: 1

Views: 4962

Answers (1)

Matt
Matt

Reputation: 46700

First thing would be to use Out-String with caution. In doing so you have destroyed the object into a string. String manipulation to extract the wanted data is possible but a waste of good PowerShell-ing when the original object contains that data in a property.

You are just looking for the cooked value? This would work in PowerShell 2.0

Get-Counter -Counter "\Process(chrome)\Working Set - Private" | 
    Select-Object -ExpandProperty CounterSamples | 
    Select-Object -ExpandProperty CookedValue

Of course you could write that with aliases and shortened property names but it gives you an idea of what the full command looks like. If you have PowerShell 3.0 you could use dot notation like this

(Get-Counter -Counter "\Process(chrome)\Working Set - Private").CounterSamples.CookedValue

Using a combination of Get-Member and viewing individual properties of objects would have been an approach to figure out this issue.

Upvotes: 5

Related Questions