1

I sometimes wished that the default output for an object would incorporate an additional property that I find useful.

For example :

$x = ps - ComputerName server1 | select -First 1
$x | fl
Id      : 880
Handles : 397
CPU     :
Name    : acnamagent

What if I want to display all those properties + the MachineName property ?

$x | select Id,Handles,CPU,Name,MachineName
Id          : 880
Handles     : 397
CPU         :
Name        : acnamagent
MachineName : server1

This works but I don't want to explicitely name all those default properties.

I tried to play with PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames but I couldn't get it to work.

Can this be done easily ?

geoced
  • 693
  • 3
  • 16

2 Answers2

2

Well, this depends on what you define as "easily". PowerShell uses XML to configure output of cmdlets (C:\windows\systems32\windowspowershell\v1.0\DotNetTypes.format.ps1xml). You create another xml file (you can't change the default one) C:\windows\systems32\windowspowershell\v1.0\Types.ps1xml. about_Types.ps1XML

Consult this: http://codingbee.net/tutorials/powershell/powershell-changing-a-command-outputs-default-formatting/

Edit: you would need to create a new PropertySet for that task. Consult these links:
https://github.com/DBremen/PowerShellScripts/blob/master/functions/Add-PropertySet.ps1
https://powershellone.wordpress.com/2015/03/06/powershell-propertysets-and-format-views/

After you've created it you would call it like that:

gps | select mypropertyset
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • That's interesting but I don't want to permanently modify the output of a specific cmdlet. I want to be able to change the output (preferably with Select-Object) on a case by case basis... – geoced Dec 07 '16 at 19:03
  • so whats the problem, you can do that with select object, whats the deal? – 4c74356b41 Dec 07 '16 at 19:04
  • 2
    I don't want to type the list of default variables like this: `$x | select Id,Handles,CPU,Name,MachineName`. I want something like `$x | select DefaultProperties,MachineName` – geoced Dec 07 '16 at 20:52
0

I ended up creating the following function to do exactly what I wanted:

<#
.Synopsis
    Selects all default properties plus those specified.
.DESCRIPTION
   In case no default properties exist, all are selected
#>
function Select-DefaultPropsPlus {
    [CmdletBinding()]
    [OutputType([PSObject])]

    Param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [PSObject] $InputObject,

        [Parameter(Mandatory = $true, Position = 1)]
        [ValidateNotNullOrEmpty()]
        [string[]] $Property
    )

    Process {
        $selectedProperties = @()

        if (($InputObject | Get-Member -Force).Name -contains "PSStandardMembers") {
            $selectedProperties = $InputObject.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames + $Property
        } else {
            $selectedProperties = *
        }

        $InputObject | Select-Object -Property $selectedProperties
    }
}
geoced
  • 693
  • 3
  • 16