Custom objects default display in PowerShell 3.0
In PowerShell 3.0 we can now create new custom objects using a hash table.
PS> [PSCustomObject]@{
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
}
One : 1
Two : 2
Three : 3
Four : 4
Five : 5
Behind the scenes, PowerShell creates a hash table and wraps it a PSCustomObject. It is way faster than using the New-Object cmdlet and it also provides consistency, while maintaining backwards compatibility. Another benefit of using PSCustomObject over New-Object is property order. PSCustomObject preserve the order of defined properties while New-Object doesn't.
PS > [PSCustomObject]@{One=1;Two=2;Three=3}
One Two Three
--- --- -----
1 2 3
PS > New-Object PSObject -Property @{One=1;Two=2;Three=3}
One Three Two
--- ----- ---
1 3 2
Notice that when the new PSCustomObject is returned, all of its properties are displayed in the console. We can control the default display property set of an object by setting a DefaultDisplayPropertySet.
$psco = [PSCustomObject]@{
One = 1
Two = 2
Three = 3
Four = 4
Five = 5
}
[string[]]$DefaultProperties = 'Two','Four','Five'
# Add the PSStandardMembers.DefaultDisplayPropertySet member
$ddps = New-Object System.Management.Automation.PSPropertySet `
DefaultDisplayPropertySet,$DefaultProperties
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$ddps
# Attach default display property set
$psco | Add-Member -MemberType MemberSet -Name PSStandardMembers `
-Value $PSStandardMembers -PassThru
Two Four Five
--- ---- ----
2 4 5
We can also define the properties of the type that will be displayed by default with the formatting cmdlets.
PS> Get-Date | Format-List
PS> Update-TypeData -TypeName System.DateTime -DefaultDisplayPropertySet `
DateTime,DayOfWeek,Year,Month,Day
PS> Get-Date | Format-List
DisplayHint : DateTime
Date : 4/9/2012 12:00:00 AM
Day : 9
DayOfWeek : Monday
DayOfYear : 100
Hour : 8
Kind : Local
Millisecond : 598
Minute : 15
Month : 4
Second : 38
Ticks : 634695561385980985
TimeOfDay : 08:15:38.5980985
Year : 2012
DateTime : Monday, April 09, 2012 8:15:38 AM
DateTime : Monday, April 09, 2012 8:15:39 AM
DayOfWeek : Monday
Year : 2012
Month : 4
Day : 9
I have put together a function that wraps the process of creating a new PSCustomObject and specifying a list of default properties to display.
function New-PSCustomObject
{
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)]
[ValidateNotNullOrEmpty()]
[System.Collections.Hashtable]$Property,
[Parameter(Position=1)]
[ValidateNotNullOrEmpty()]
[Alias('dp')]
[System.String[]]$DefaultProperties
)
$psco = [PSCustomObject]$Property
# define a subset of properties
$ddps = New-Object System.Management.Automation.PSPropertySet `
DefaultDisplayPropertySet,$DefaultProperties
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]$ddps
# Attach default display property set
$psco | Add-Member -MemberType MemberSet -Name PSStandardMembers `
-Value $PSStandardMembers -PassThru
}
# usage example
# define 5 properties
PS> $property = @{Name='My Object';One=1;Two=2;Three=3;Four=4}
# create the object and set just three properties for default display
PS> New-PSCustomObject -Property $property -DefaultProperties Name,Two,Four
Two Four Five
--- ---- ----
2 4 5