At the end of the following code, I am using DefaultDisplayPropertySet to display only specific properties. Although this works perfectly, running this code within the foreach loop seems excessive. Without resorting to using ps1xml files, is it possible to define the default display properties without doing on each object, for example on the class? Thanks.
foreach ($Object in $Objects) {
#Store desired properties in the new object
$objDetails = [objInfo]::new() #the class is defined earlier, of course
$objDetails.Prop1 = $Object.Prop1
$objDetails.Prop2 = $Object.Prop2
$objDetails.Prop3 = $Object.Prop3
$objDetails.Prop4 = $Object.Prop4
$objDetails.Prop5 = $Object.Prop5
#Set the default properties to be displayed
$defaultProperties = @('Prop1', 'Prop2', 'Prop3')
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet', [string[]]$defaultProperties)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$objDetails | Add-Member MemberSet PSStandardMembers $PSStandardMembers
Write-Output $objDetails
}