Specifying non-default properties with Select-Object
I am a Powershell practice and the question I have assigned is as follows:
Write a pipeline to fetch a process when cpu usage is greater than zero, return properties not shown in the default view, and sort the results in descending CPU order
Currently I think I have completed everything except the custom property requirements, which I believe can be done by the Select-Object cmdlet.
The code I have so far:
Get-Process | Where-Object {$_.CPU -gt 0 } | Select-Object -Property * | sort -Descending
I know that asterisks are a nickname for selecting all properties, but I don't know how to set up a boolean check if a property is in the default view or not. Can someone please help me?
source to share
Look at the parameter -ExcludeProperty
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 return value in the output Get-Process
. Destroying everything:
# 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
source to share