SOLVED

Filter by nested properties of type timespan

Copper Contributor

Hi folks,

I am new to powershell and I wanted to understand the concepts behind it and play a bit with filtering by objects resp. by properties. For example I wanted to filter the output of get-process and only show processes with TotalProcessorTime.Seconds greater than 1. Sorry for that stupid example, but I only chose a random property and sadly this does not work :)

 

 

get-process | where TotalProcessorTime.Seconds -gt 1

 

 

For the above command I got an empty result set.

But when I look at TotalProcessorTime of some processes I see that there are some processes that TotalProcessorTime.Second is greater than 1.

 

 

PS C:\Windows\system32> get-process | Select-Object TotalProcessorTime -First 5

TotalProcessorTime
------------------
00:00:00.1406250
00:00:00.0781250
00:00:01.9531250
00:00:00.0468750
00:00:05.1250000

 

 

So what I am doing wrong? Do I have to take a different approach?

I did a check with a variable of type timespan and in this example I can read the property Seconds:

 

 

 

PS C:\Windows\system32> $ts = New-TimeSpan -Hours 1 -Minutes 25 -Seconds 5
PS C:\Windows\system32> $ts.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     TimeSpan                                 System.ValueType

PS C:\Windows\system32> $ts.Seconds
5

 

 

 

 

 

2 Replies
best response confirmed by SimonSays1 (Copper Contributor)
Solution

Hello @SimonSays1,

You are correct TotalProcessorTime is TimeSpan and you can use .seconds property to view the seconds.

The problem that you are facing is related to pipeline processing. In your Where condition you need to use $_ to reference the object that being passed through the pipeline.

get-process | where {$_.TotalProcessorTime.Seconds -gt 1}

Reference: about_Pipelines

Hope that helps.

 

Hi @AndySvints,

thank you very much! I thought I would not need necessarily the $_ to reference the object. And thank for the reference!
1 best response

Accepted Solutions
best response confirmed by SimonSays1 (Copper Contributor)
Solution

Hello @SimonSays1,

You are correct TotalProcessorTime is TimeSpan and you can use .seconds property to view the seconds.

The problem that you are facing is related to pipeline processing. In your Where condition you need to use $_ to reference the object that being passed through the pipeline.

get-process | where {$_.TotalProcessorTime.Seconds -gt 1}

Reference: about_Pipelines

Hope that helps.

 

View solution in original post