Public/Add-DefaultPropertySetProperty.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function Add-DefaultPropertySetProperty {
    <#
    .SYNOPSIS
    Adds Properties to the Default Display Property Set of an Object.
 
    .DESCRIPTION
    The Add-DefaultPropertySetProperty function adds Properties to the Default
    Display Property Set of a PSCustomObject or collection of PSCustomObjects.
 
    If the provided Object does not currently have a Default Display Property
    Set, this function will terminate and advise using the Set-DefaultPropertySet
    function instead.
 
    .PARAMETER Object
    PSCustomObject(s) with an existing Default Display Property Set.
 
    .PARAMETER Property
    The name of the properties to be added to the Default Display Property Set.
 
    .EXAMPLE
    Add-DefaultPropertySetProperty -Object $DemoObject1 -Property 'Name', 'Date'
 
    This command adds the properties 'Name' and 'Date' to $DemoObject1's Default
    Display Property Set.
 
    Note that $DemoObject1 should already have a Default Display Property Set,
    otherwise this function will generate an error.
 
    .EXAMPLE
    $DemoObject1, $DemoObject2 | Add-DefaultPropertySetProperty -Property 'Date'
 
    This command takes two objects as input from the pipeline and adds the property
    'Date' to both of their Default Display Property Sets.
 
    Note that $DemoObject1 should already have a Default Display Property Set,
    otherwise this function will generate an error.
 
    .LINK
    https://github.com/Windos/DefaultPropertySetter
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory,
                   ValueFromPipeline)]
        [PSCustomObject[]] $Object,

        [Parameter(Mandatory)]
        [String[]] $Property
    )

    Begin {}

    Process {
        foreach ($Obj in $Object) {
            if ($Obj.PSStandardMembers) {
                foreach ($Prop in $Property) {
                    $Obj.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames.Add($Prop)
                }
            } else {
                $ErrorMsg = 'Cannot add Properties as a Default Display Property Set does not exist on the Object. '
                $ErrorMsg += 'Please run the Set-DefaultPropertySet function instead.'
                throw $ErrorMsg
            }
        }
    }

    End {}
}