Here's a Join-String method that will work in older methods of PowerShell
function Join-String {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)] [string[]]$StringArray,
$Separator=",",
[switch]$DoubleQuote=$false
)
BEGIN{
$joinArray = [System.Collections.ArrayList]@()
}
PROCESS {
foreach ($astring in $StringArray) {
$joinArray.Add($astring) | Out-Null
}
}
END {
$Object = [PSCustomObject]@{}
$count = 0;
foreach ($aString in $joinArray) {
$name = "ieo_$($count)"
$Object | Add-Member -MemberType NoteProperty -Name $name -Value $aString;
$count = $count + 1;
}
$ObjectCsv = $Object | ConvertTo-Csv -NoTypeInformation -Delimiter $separator
$result = $ObjectCsv[1]
if (-not $DoubleQuote) {
$result = $result.Replace('","',",").TrimStart('"').TrimEnd('"')
}
return $result
}
}
It can be invoked with array parameter or passthrough
Join-String @("file1.txt","file2.txt","file3.txt") -DoubleQuote
Output:
"file1.txt","file2.txt","file3.txt"
or as pass-thru:
@("file1.txt","file2.txt","file3.txt") | Join-String -DoubleQuote
Output:
"file1.txt","file2.txt","file3.txt"
Without -DoubleQuote
@("file1.txt","file2.txt","file3.txt") | Join-String
Output:
file1.txt,file2.txt,file3.txt
or with a custom separator, say a semicolon
@("file1.txt","file2.txt","file3.txt") | Join-String -DoubleQuote -Separator ";"
Output:
"file1.txt";"file2.txt";"file3.txt"
'"' + ($myArray -join '","') + '"'