Skip to content

Instantly share code, notes, and snippets.

@michaeltlombardi
Last active November 22, 2022 12:34
Show Gist options
  • Save michaeltlombardi/a219acf2e82d093067fa to your computer and use it in GitHub Desktop.
Save michaeltlombardi/a219acf2e82d093067fa to your computer and use it in GitHub Desktop.
Function for Adding a Default Property Display Set to PSCustomObjects.
Function Set-DefaultPropertyDisplaySet {
<#
.Synopsis
Adds a Default Property Display Set to a PSCustomObject.
.Description
Adds a Default Property Display Set to a PSCustomObject. The default display will be formatted as a table; the columns will be in the
same order as they are passed to the function.
.Parameter InputObject
The PSCustomObject to which you are adding a Default Property Display Set.
.Parameter DefaultPropertyList
The list of properties that will be used for the PSCustomObject's default display. Each property will be the header of a column and the
columns will be in the same order as they are specified here.
.Example
Set-DefaultPropertyDisplaySet -InputObject $Servers -DefaultPropertyList "ComputerName","Site","Enclave","Role"
The above command will update the $Servers objects with the default property display set; If called at the prompt, the objects will be
displayed formatted as a table with the columns ComputerName, Site, Enclave, and Role ordered the same.
.Example
$Servers | Set-DefaultPropertyDisplaySet -DefaultPropertyList "ComputerName","Site","Enclave","Role"
The above command will update the $Servers objects with the default property display set; If called at the prompt, the objects will be
displayed formatted as a table with the columns ComputerName, Site, Enclave, and Role ordered the same.
.Notes
The code in the "Begin" block was discovered at the addresses below and all credit belongs to the authors:
* [Boe Prox](http://learn-powershell.net/2013/08/03/quick-hits-set-the-default-property-display-in-powershell-on-custom-objects/)
* [Rakhesh Sasidharan](http://rakhesh.com/powershell/naming-powershell-custom-objects-and-setting-their-default-display-properties/)
#>
param(
[Parameter(Mandatory=$true,
HelpMessage="Enter the Object or Objects you want to add a default property display set to, separated by a comma.",
ValueFromPipeline=$true)]
[Alias("Object","obj")]
[PSCustomObject[]]$InputObject,
[Parameter(Mandatory=$true,
HelpMessage="Enter the properties you want to assign to the default display set, separated by a comma.")]
[Alias("Property","dpl")]
[string[]]$DefaultPropertyList,
[switch]$PassThru
)
Begin {
# Use the list of property names to create the Default Property Display Set.
$DefaultPropertyDisplaySet = New-Object System.Management.Automation.PSPropertySet("DefaultDisplayPropertySet",[string[]]$DefaultPropertyList)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultPropertyDisplaySet)
}
Process {
ForEach($Object in $InputObject){
# Add the default property display set to each object.
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $PSStandardMembers -InputObject $Object -PassThru:$PassThru
}
}
End {
# There is no post-processing to perform.
}
}
Set-Alias -Name sdpds -Value Set-DefaultPropertyDisplaySet
@ChrisMagnuson
Copy link

I would like to use this code, is there a way you could add a comment indicating what license this is covered by?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment