Last active
December 23, 2024 19:22
-
-
Save scriptingstudio/d874b9616df567f7de768dfca24c6b7d to your computer and use it in GitHub Desktop.
A missing Windows PowerShell function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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