functions/text/Join-Strings.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
function Join-Strings {
    [Cmdletbinding()]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'String')]
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'Object')]
        [object[]]$Value,
        [Parameter(Mandatory = $true, ParameterSetName = 'Object')]
        [string]$ExpandProperty,
        [Parameter(Mandatory = $true)]
        [string]$Separator
    )    

    begin {
        $stringCache = ""
    }
    process {
        switch ($PsCmdlet.ParameterSetName) {
            'String' {
                $stringCache += $Value
            }
            'Object' {
                $stringCache += ($Value | Select-Object -ExpandProperty $ExpandProperty)
            }
        }
        $stringCache += $Separator
    }
    end {
        $stringCache.Trim().TrimEnd(',')
    }
}