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
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(Mandatory,ValueFromPipeline)]$inputObject,
        [Parameter(Mandatory,Position=0)][ScriptBlock]$scriptBlock
    )

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

}