EZOut | Making Select-Object Simpler with PropertySets

Making Select-Object Simpler with PropertySets [1 of 5]

The PowerShell Types system has a lot of really underused capabilities. One particularily interesting one is PropertySets.

A Property Set is a named view of a type's data, which will only pick up those properties.

You use them with Select-Object. A built in one is the PSConfiguration view on the output of Get-Process

Get-Process | Select-Object PSConfiguration            
            

Making Select-Object Simpler with PropertySets [2 of 5]

You can see all of the PropertySets built into PowerShell with the Get-PropertySet command from EZOut

Get-PropertySet            
            

Making Select-Object Simpler with PropertySets [3 of 5]

To create new ones, you can use the command ConvertTo-TypePropertySet. You pipe in a Select-Object result to this command in order to create a result set. This small pipeline creates a property set for file times.


Get-ChildItem |             
            Select-Object Name, LastWriteTime, CreationTime |            
            ConvertTo-TypePropertySet -Name FileTimes            
                        

Making Select-Object Simpler with PropertySets [4 of 5]

To dynamically add it, pipe it into Out-TypeData and Add-TypeData

Get-ChildItem |             
            Select-Object Name, LastWriteTime, CreationTime |            
            ConvertTo-TypePropertySet -Name FileTimes |             
            Out-TypeData |            
            Add-TypeData            
            

Making Select-Object Simpler with PropertySets [5 of 5]

Now we can ask for the FileTimes property set when we use Select-Object

Get-ChildItem |             
    Select-Object filetimes