Question:
Here is a blog post from Kirk Munro that explains how a script can set the DefaultDisplayPropertySet on its output objects:
Essential PowerShell: Define default properties for custom objects
His technique and example code doesn’t appear to work in PowerShell v2. (Note, I have PowerTab and PSCX installed–perhaps those could be interfering.)
Anyone know how to get this to work on PowerShell v2?
UPDATE:
Here’s the example from the blog post, which isn’t working for me (note, I’ve corrected the single quote characters):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
$myObject = New-Object PSObject $myObject | Add-Member NoteProperty Name 'My Object' $myObject | Add-Member NoteProperty Property1 1 $myObject | Add-Member NoteProperty Property2 2 $myObject | Add-Member NoteProperty Property3 3 $myObject | Add-Member NoteProperty Property4 4 $myObject | Add-Member NoteProperty Property5 5 $myObject ## Output: # Name : My Object # Property1 : 1 # Property2 : 2 # Property3 : 3 # Property4 : 4 # Property5 : 5 $defaultProperties = @('Name','Property2','Property4') $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultProperties) $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) $myObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers $myObject ## Output: # Name : My Object # Property1 : 1 # Property2 : 2 # Property3 : 3 # Property4 : 4 # Property5 : 5 |
The output should not be the same after adding DefaultDisplayPropertySet (i.e., it should only have Name, Property2, and Property4).
Answer:
Can you give an example of your non-working code? This should work perfectly in v2, if not, you’ve found a bug.
UPDATE:
(removed comments about quoting)
I’ve confirmed with the powershell team that this is indeed a regression (bug).
You can vote on the issue’s importance to you here:
https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=487938
Thanks,
-Oisin
(powershell MVP)