In Powershell v3, is there a way to set the default display property of a custom object to be expanded?
<p>I have a base set of properties in a custom object that is retrieved from a database. It looks like this:</p>

<pre><code>&gt; $baseObject | Format-List


Id       : 199fc2fb-249e-40f8-8d52-ab05b38b5473
BaseType : container
Format   : Employee Listing
QueryUrl : https://datacenter/api/v1/employees.json?id=199fc2fb-249e-40f8-8d52-ab05b38b5473
</code></pre>

<p>When I <code>Invoke-RestMethod</code> on <code>$baseObject.QueryUrl</code>, it returns the same data in with a new member added which contains an employee listing. The results are similar to the following:</p>

<pre><code>&gt; $employeeObject = Invoke-RestMethod -Uri $baseObject.QueryUrl

&gt; $employeeObject

Id              : 199fc2fb-249e-40f8-8d52-ab05b38b5473
BaseType        : container
Format          : Employee Listing
QueryUrl        : https://datacenter/api/v1/employees.json?id=199fc2fb-249e-40f8-8d52-ab05b38b5473
EmployeeListing : {@{Name=Keanu Reeves; Age=51; Specialty=Security}, @{Name=Morgan Freeman; Age=79; Specialty=Fixed Operations}, @{Name=Miyuki
              Miyazaki; Age=37; Specialty=Executive}}
</code></pre>

<p>In order to view this information in a formatted manner, users essentially have to run the following command:</p>

<pre><code>&gt; $employeeObject | Select -Expand EmployeeListing

Name            Age Specialty
----            --- ---------
Keanu Reeves     51 Security
Morgan Freeman   79 Fixed Operations
Miyuki Miyazaki  37 Executive
</code></pre>

<p>I have used <code>DefaultDisplayPropertySet</code> elsewhere (<a href="https://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/" rel="nofollow noreferrer">as demonstrated here</a>) to make it so custom objects are formatted nicely in a table. I am doing the same to <code>EmployeeListing</code> in this case.</p>

<p>The problem I'm having is that I have scripts that will query multiple <code>$baseObject.QueryUrl</code> values, manipulate the data from all of the results, and display the results to the user. Some of the query URLs only return one <code>EmployeeListing</code> entry, although most of them return many. As it is now, the nice output formatting is impossible to produce. Without setting the <code>DefaultDisplayPropertySet</code> to <code>EmployeeListing</code>, results are displayed like so:</p>

<pre><code>&gt; Get-AllEmployees

Id              : 199fc2fb-249e-40f8-8d52-ab05b38b5473
BaseType        : container
Format          : Employee Listing
QueryUrl        : https://datacenter/api/v1/employees.json?id=199fc2fb-249e-40f8-8d52-ab05b38b5473
EmployeeListing : {@{Name=Keanu Reeves; Age=51; Specialty=Security}, @{Name=Morgan Freeman; Age=79; Specialty=Fixed Operations}, @{Name=Miyuki
                  Miyazaki; Age=37; Specialty=Executive}}

Id              : 6444128f-a179-4081-a123-d54b3460fa25
BaseType        : container
Format          : Employee Listing
QueryUrl        : https://datacenter/api/v1/employees.json?id=6444128f-a179-4081-a123-d54b3460fa25
EmployeeListing : {@{Name=Jason Bourne; Age=42; Specialty=Security}, @{Name=Anton Chigurh; Age=55; Specialty=Sales}, @{Name=Patrick Bateman; Age=27;
                  Specialty=Mergers &amp; Acquisitions}}
</code></pre>

<p>Even if I set <code>DefaultDisplayPropertySet</code> to only show <code>EmployeeListing</code>, it always shows the hashtable format <em>for each individual resulting object</em>:</p>

<pre><code>&gt; Get-AllEmployees

EmployeeListing
---------------
{@{Name=Keanu Reeves; Age=51; Specialty=Security}, @{Name=Morgan Freeman; Age=79; Specialty=Fixed Operations}, @{Name=Miyuki Miyazaki; Age=37; Spe...
{@{Name=Jason Bourne; Age=42; Specialty=Security}, @{Name=Anton Chigurh; Age=55; Specialty=Sales}, @{Name=Patrick Bateman; Age=27; Specialty=Merge...
</code></pre>

<p>I want them to be displayed as a table. I could easily solve this by piping the results of all of these queries through to <code>Select -Expand EmployeeListing</code> as each listing is returned from the pipeline in <code>Get-AllEmployees</code>. However, <strong>it is important that the other values be present in the returned object</strong> (<code>Id</code>, <code>BaseType</code>, <code>Format</code>, etc.) <strong>even if they aren't displayed.</strong> This is because there are other scripts and programs that expect the objects to have the data contained in them this way (and that's why the API I'm calling returns them as such.)</p>

<p>In essence, <strong>this</strong> is what I'm trying to accomplish:</p>

<pre><code>&gt; Get-AllEmployees

Name            Age Specialty
----            --- ---------
Keanu Reeves     51 Security
Morgan Freeman   79 Fixed Operations
Miyuki Miyazaki  37 Executive
Jason Bourne     42 Security
Anton Chigurh    55 Sales
Patrick Bateman  27 Mergers &amp; Acquisitions



&gt; Get-AllEmployees | Format-List -Property *

Id              : 199fc2fb-249e-40f8-8d52-ab05b38b5473
BaseType        : container
Format          : Employee Listing
QueryUrl        : https://datacenter/api/v1/employees.json?id=199fc2fb-249e-40f8-8d52-ab05b38b5473
EmployeeListing : {@{Name=Keanu Reeves; Age=51; Specialty=Security}, @{Name=Morgan Freeman; Age=79; Specialty=Fixed Operations}, @{Name=Miyuki
                  Miyazaki; Age=37; Specialty=Executive}}

Id              : 6444128f-a179-4081-a123-d54b3460fa25
BaseType        : container
Format          : Employee Listing
QueryUrl        : https://datacenter/api/v1/employees.json?id=6444128f-a179-4081-a123-d54b3460fa25
EmployeeListing : {@{Name=Jason Bourne; Age=42; Specialty=Security}, @{Name=Anton Chigurh; Age=55; Specialty=Sales}, @{Name=Patrick Bateman; Age=27;
                  Specialty=Mergers &amp; Acquisitions}}
</code></pre>

<p>Is this possible?</p>

<p>For what it's worth, this is a problem that (I believe) could probably be solved by using Powershell v5 with its more feature-rich class/type system, but unfortunately, I'm locked to v3.</p>

<hr>

<p><strong>UPDATE:</strong> Still not there yet, but I appear to be getting closer.</p>

<p>After reading the MSDN documentation provided in a link by <a href="https://stackoverflow.com/questions/39238264/in-powershell-v3-is-there-a-way-to-set-the-default-display-property-of-a-custom#comment65814575_39238264">Trevor Sullivan</a>, I wrote a <a href="https://gist.github.com/anonymous/d13c7ac71c853697c6b44ff23c015533" rel="nofollow noreferrer">simple object display formatting module</a> so I could dynamically provide format data to objects instead of having to write .ps1xml files for every conceivable type. (It's not super efficient yet, but it works as a replacement for the time being.) My <code>Get-AllEmployees</code> script looks something like this now:</p>

<pre><code>...
$allListings = Get-AllQueryUrls -Format "Employee Listing"
foreach ($employeeListing in $allListings)
{
  $result = $employeeListing
  $result | Set-ObjectDisplayFormat -TypeName "EmployeeListingRecord" -Property @{N="Name"; E={$_.EmployeeListing.Name}},@{N="Age"; E={$_.EmployeeListing.Age}},@{N="Specialty"; E={$_.EmployeeListing.Specialty}}
  $result
}
</code></pre>

<p>Now, when I execute <code>Get-AllEmployees</code>, the output looks like this:</p>

<pre><code>&gt; Get-AllEmployees

Name                                            Age          Specialty
----                                            ---          ---------
{Keanu Reeves, Morgan Freeman, Miyuki Miyazaki} {51, 79, 37} {Security, Fixed Operations, Executive}
{Jason Bourne, Anton Chigurh, Patrick Bateman}  {42, 55, 27} {Security, Sales, Mergers &amp; Acquisitions}
</code></pre>

<p>I count this as progress, but it's <em>far</em> from correct, so I'm still looking for an answer.</p>