Selecting Property order

If you run

Get-WmiObject -Class Win32_ComputerSystem

you get a few properties displayed

Domain              : WORKGROUP
Manufacturer        : Hewlett-Packard
Model               : HP G60 Notebook PC
Name                : RSLAPTOP01
PrimaryOwnerName    : Richard
TotalPhysicalMemory : 2951139328

 

Now if you want all properties you need

Get-WmiObject -Class Win32_ComputerSystem | fl *

or

Get-WmiObject -Class Win32_ComputerSystem | select *

If you want a particular set of properties then this will work

Get-WmiObject -Class Win32_ComputerSystem | select Name, SystemType, Manufacturer, Model, BootupState

 

A comment was left on this post

http://msmvps.com/blogs/richardsiddaway/archive/2011/12/23/1792823.aspx

regarding how the reader wanted a specific set of properties displayed first and then all of the other properties in any appropriate order

You might think that this would work

Get-WmiObject -Class Win32_ComputerSystem | select Name, SystemType, Manufacturer, Model, BootupState, *

but in fact we get a series of errors and then all of the properties in the standard order.

Select-Object has  –Property and –ExcludeProperty parameters but they won’t help us as we want to display all properties

One thing to remember is that you can do this

$p = "Name", "SystemType", "Manufacturer", "Model", "BootupState"
Get-WmiObject -Class Win32_ComputerSystem | select $p

 

Define the list of properties in an array and use that as the selection.  This because –Property is a positional property and takes position 1 so is assumed if no parameter name is supplied. What we are doing is this

$p = "Name", "SystemType", "Manufacturer", "Model", "BootupState"
Get-WmiObject -Class Win32_ComputerSystem | select -Property $p

This enables us to write a function that takes an object and list of properties as input and creates a selection list based on the object’s full property list. The properties defined to the function are selected first and then all other properties in the order that Get-Member supplies them.

function Select-Order {            
[CmdletBinding()]            
param (            
[parameter(Position=0,            
   ValueFromPipeline=$true)]            
   $InputObject,             
[string[]]$firstprop            
)            
            
PROCESS {            
$proplist = $firstprop            
            
$Inputobject | Get-Member -MemberType Property |            
foreach {            
 if ($firstprop -notcontains $_.Name){            
   $proplist += $_.Name            
 }            
}            
            
$InputObject | select -Property $proplist            
}}


 



You can use it like this



$p = "Name", "SystemType", "Manufacturer", "Model", "BootupState"



Get-WmiObject Win32_ComputerSystem | Select-Order -firstprop $p



or



Get-WmiObject Win32_ComputerSystem | Select-Order -firstprop "Name", "SystemType", "Manufacturer", "Model", "BootupState"



or



$o = Get-WmiObject Win32_ComputerSystem                                   
Select-Order -InputObject $o -firstprop $p 



or



Select-Order -InputObject $o -firstprop "Name", "SystemType", "Manufacturer", "Model", "BootupState"

One Response to Selecting Property order

Leave a Reply