Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set an object's DefaultDisplayPropertySet in a PowerShell v2 script?

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):
$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).

like image 217
totorocat Avatar asked Sep 02 '09 19:09

totorocat


People also ask

How do I run PowerShell v2?

When you start Windows PowerShell the newest version starts by default. To start Windows PowerShell with the Windows PowerShell 2.0 Engine, use the Version parameter of PowerShell.exe . You can run the command at any command prompt, including Windows PowerShell and Cmd.exe.

What is PSCustomObject PowerShell?

The [pscustomobject] type accelerator was added in PowerShell 4.0. Prior to adding this type accelerator, creating an object with member properties and values was more complicated. Originally, you had to use New-Object to create the object and Add-Member to add properties. For example: PowerShell Copy.


2 Answers

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)

like image 97
x0n Avatar answered Sep 24 '22 14:09

x0n


Here is a solution I created to work around this issue:

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 the 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 via PoshCode: Set-PSObjectDefaultProperties

like image 39
russellds Avatar answered Sep 23 '22 14:09

russellds