Reputation: 722
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 ?
Upvotes: 1
Views: 1058
Reputation: 722
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
}
}
Upvotes: 0
Reputation: 72171
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
Upvotes: 2