Private/FormatStringOut.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
function FormatStringOut () {
<#
.SYNOPSIS
Change what is shown when the supplied object is cast to a string. Use $this to reference the supplied object
.EXAMPLE
[String](FormatStringOut (Get-Item .) {$this.name})
#>

    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline)]$inputObject,
        [Parameter(Mandatory,Position=0)][ScriptBlock]$scriptBlock
    )

    process {
        if ($inputObject) {
            $AddMemberParams = @{
                InputObject = $inputObject
                MemberType = 'ScriptMethod'
                Name = 'ToString'
                Force = $true
                Value = $scriptBlock
            }
            Add-Member @AddMemberParams
        }
    }

}