Can you set the DefaultDisplayPropertySet object in a PowerShell v2 script?

Here is a blog post from Kirk Munro explaining how a script can set DefaultDisplayPropertySet in its output objects:

Essential PowerShell: Defining Default Properties for Custom Objects

His method and code sample does not seem to work in PowerShell v2. (Please note that I have PowerTab and PSCX installed - maybe this may interfere.)

Does anyone know how to make this work on PowerShell v2?


UPDATE: Here is an example from a blog post that doesn't work for me (note I fixed single quote characters):
$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 match after adding the DefaultDisplayPropertySet (i.e. it should only have a name, property2 and property4).

+7
powershell
source share
3 answers

Can you give an example of your non-working code? This should work fine in v2, if not, you have found an error.

UPDATE:

(removed quoting comments)

I confirmed with the powershell team that this is indeed a regression (error).

You can vote on an issue important to you here:

https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=487938

Thanks,

-Oisin (MVP PowerSpace)

+6
source share

Here is the solution I created to solve this problem:

 function Set-PSObjectDefaultProperties { param( [PSObject]$Object, [string[]]$DefaultProperties ) $name = $Object.PSObject.TypeNames[0] $xml = "<?xml version='1.0' encoding='utf-8' ?><Types><Type>" $xml += "<Name>$($name)</Name>" $xml += "<Members><MemberSet><Name>PSStandardMembers</Name><Members>" $xml += "<PropertySet><Name>DefaultDisplayPropertySet</Name><ReferencedProperties>" foreach( $default in $DefaultProperties ) { $xml += "<Name>$($default)</Name>" } $xml += "</ReferencedProperties></PropertySet></Members></MemberSet></Members>" $xml += "</Type></Types>" $file = "$($env:Temp)\$name.ps1xml" Out-File -FilePath $file -Encoding "UTF8" -InputObject $xml -Force $typeLoaded = $host.Runspace.RunspaceConfiguration.Types | where { $_.FileName -eq $file } if( $typeLoaded -ne $null ) { Write-Verbose "Type Loaded" Update-TypeData } else { Update-TypeData $file } 

}

Now you can use the following to create your custom object and set default properties in PowerShell V2:

 $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') Set-PSObjectDefaultProperties $myObject $defaultProperties $myObject ## Output: #Name Property2 Property4 #---- --------- --------- #My Object 2 4 

It is also available through PoshCode: Set-PSObjectDefaultProperties

+2
source share

I get the same results as you - it displays all 5 properties. I am running Powershell 2.0 RC on Vista. I do not have PowerTab or PSCX.

0
source share

All Articles