Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active February 28, 2024 02:54
Show Gist options
  • Save scriptingstudio/d874b9616df567f7de768dfca24c6b7d to your computer and use it in GitHub Desktop.
Save scriptingstudio/d874b9616df567f7de768dfca24c6b7d to your computer and use it in GitHub Desktop.
A missing Windows PowerShell function.
function Join-String {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]$Inputobject,
[string]$Property, [string]$Separator='',
[string]$OutputPrefix, [string]$OutputSuffix,
[switch]$SingleQuote, [switch]$DouleQuote,
[string]$FormatString
)
begin {
$content = [System.Collections.Generic.List[object]]::new()
}
process {
if ($Inputobject) {
if ($Property) {$content.AddRange(@($Inputobject.$Property))}
else {$content.AddRange(@($Inputobject))}
}
}
end {
if (-not $content.count) {return}
$q = if ($DouleQuote) {'"'} elseif ($SingleQuote) {"'"} else {''}
$r = $(foreach ($p in $content) {
if ($p -or $p -eq 0) {
if ($FormatString) {$p = $FormatString -f $p}
"$q$p$q"
}
}) -join $Separator
if ($OutputPrefix) {$r = "$OutputPrefix$r"}
if ($OutputSuffix) {$r = "$r$OutputSuffix"}
$r
}
} # END Join-String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment