Skip to content

Instantly share code, notes, and snippets.

@powercode
Last active November 19, 2023 22:29
Show Gist options
  • Save powercode/7531b3b27434b035dd259d1d62bef2b8 to your computer and use it in GitHub Desktop.
Save powercode/7531b3b27434b035dd259d1d62bef2b8 to your computer and use it in GitHub Desktop.
#requires -Version 5.0
using namespace System.Management.Automation
using Namespace System.Collections
using Namespace System.Collections.Generic
<#
.SYNOPSIS
Combine items from the pipeline into a string, optionally adding single or double quotes
.DESCRIPTION
The Join-Item command joins items from the pipeline into a string, optionally adding single or double quotes
and inserts a delimiter between the items
.EXAMPLE
1..10 | Join-Item -DoubleQuote
This example shows how to join numbers with double quotes
output: "1","2","3","4","5","6","7","8","9","10"
.EXAMPLE
1..10 | Join-Item -Delimiter ';'
This example shows how to join numbers with a non default Delimiter
output: 1;2;3;4;5;6;7;8;9;10
.EXAMPLE
1..10 | Join-Item -Delimiter ', ' -Quote
This example shows how to join numbers with a Delimiter ', ' with the items quoted
output: '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'
.EXAMPLE
Get-ChildItem | Join-Item -Property Name -Quote
This example demonstrates how to join the value of a named property of the pipeline objects into
a quoted, comma separated, string
output: 'file1.ps1','file2.ps1','file3.ps1'
#>
function Join-Item {
[Alias('ji')]
[CmdletBinding(DefaultParameterSetName = 'Default')]
[OutputType([string])]
param(
[Parameter(Position = 0)]
[ArgumentCompleter([JoinItemCompleter])]
[string] $Delimiter = ',',
[Parameter(Position = 1)]
[string] $PropertyName,
[Parameter(ParameterSetName = 'Quote')]
[switch] $Quote,
[Parameter(ParameterSetName = 'DoubleQuote')]
[switch] $DoubleQuote,
[Parameter(ValueFromPipeline, Mandatory)]
[PSObject[]] $InputObject
)
begin {
Set-StrictMode -Version Latest
$inputObjects = [List[PSObject]]::new(100)
}
process {
$inputObjects.AddRange($InputObject)
}
end {
$isPropertyName = $PSBoundParameters.ContainsKey('PropertyName')
for ($i = 0; $i -lt $inputObjects.Count; ++$i) {
if ($isPropertyName) {
$value = $inputObjects[$i].$PropertyName.ToString()
}
else {
$value = $inputObjects[$i].ToString()
}
if ($Quote) {
$value = "'$value'"
}
elseif ($DoubleQuote) {
$value = "`"$value`""
}
$inputObjects[$i] = $value
}
$str = $InputObjects -join $Delimiter
$PSCmdlet.WriteObject($str)
}
}
class JoinItemCompleter : IArgumentCompleter {
[IEnumerable[CompletionResult]] CompleteArgument(
[string] $CommandName,
[string] $ParameterName,
[string] $wordToComplete,
[Language.CommandAst] $commandAst,
[IDictionary] $fakeBoundParameters
) {
$result = [List[CompletionResult]]::new(5)
$this.AddResultIf(', ', 'Comma-Space', $wordToComplete, $result)
$this.AddResultIf(';', 'Semi-Colon', $wordToComplete, $result)
$this.AddResultIf('; ', 'Semi-Colon-Space', $wordToComplete, $result)
$this.AddResultIf('`r`n', 'Newline', $wordToComplete, $result)
$this.AddResultIf(',', 'Comma', $wordToComplete, $result)
$this.AddResultIf('-', 'Dash', $wordToComplete, $result)
$this.AddResultIf(' ', 'Space', $wordToComplete, $result)
return $result
}
[void] AddResultIf([string] $complete, [string] $description, [string] $wordToComplete, [List[CompletionResult]] $result) {
if ($complete.StartsWith($wordToComplete, [System.StringComparison]::OrdinalIgnoreCase)) {
$result.Add([CompletionResult]::new("`"$complete`"", $description, [CompletionResultType]::ParameterValue, "'$complete' - $description"))
}
}
}
<#
Describe 'Join-Item tests' {
It 'Should default join with comma' {
1..4 | Join-Item | Should BE '1,2,3,4'
}
It 'can double quote with default delimiter' {
1..4 | Join-Item -DoubleQuote | Should BE '"1","2","3","4"'
}
It 'can quote with default delimiter' {
1..4 | Join-Item -Quote | Should BE "'1','2','3','4'"
}
It 'Should join with Delimiter' {
1..4 | Join-Item -Delimiter ', ' | Should BE '1, 2, 3, 4'
}
It 'Should join Property with Delimiter' {
@(
@{Name = 'Ken'}
@{Name = 'Barbie'}
@{Name = 'Donald'}
) | Join-Item -Delimiter ', ' -Property Name -DoubleQuote | Should be '"Ken", "Barbie", "Donald"'
}
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment