3

I am practicing Powershell and the question that I have been assigned is as follows:

Write a pipeline to get-process where cpu utilization is greater than zero, return properties not shown in default view and sort the results by CPU descending

Currently I think I have completed everything except for the requirement of non default properties, which I assume can be done by the Select-Object cmdlet.

Code I have so far:

Get-Process | Where-Object {$_.CPU -gt 0 } | Select-Object -Property * | sort -Descending

I know the asterisks is a moniker for selecting all properties but I don't know how to set up a Boolean check for if the property is in the default view or not. Can someone please help me?

Justin Reid
  • 119
  • 1
  • 9
  • 1
    It might be a question of interpretation of the wording. It could be interpreted that returning properties shown in the default view, *plus* the extra ones not shown is what's required. Have a look at `Get-Process | Get-Member` to see if there's anything about the properties that flags them as default. – mjsqu Jun 22 '17 at 05:02

1 Answers1

3

Look at the -ExcludeProperty option in Select-Object:

Get-Process | `
Where-Object {$_.CPU -gt 0 } | `
Select-Object -Property * -ExcludeProperty $(Get-Process)[0].PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | `
sort -Descending | format-table

Where:

$(Get-Process)[0].PSStandardMembers.DefaultDisplayPropertySet).ReferencedPropertyNames

is the default list for the first returned value in the Get-Process output. Breaking the whole thing down:

# First process
$fp = $(Get-Process)[0]
# Standard Members
$PSS = $fp.PSStandardMembers
# Default Display Property Set
$DDPS = $PSS.DefaultDisplayPropertySet
# Names of those properties in a list, that you can pass to -ExcludeProperty
$Excl = $DDPS.ReferencedPropertyNames
$Excl

# Command using variables
Get-Process | `
Where-Object {$_.CPU -gt 0 } | `
Select-Object -Property * -ExcludeProperty $Excl | `
sort -Descending | format-table
mjsqu
  • 5,151
  • 1
  • 17
  • 21