diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs
index dd878bd72c..a2b7f03ce7 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs
@@ -2,8 +2,10 @@
// Licensed under the MIT License.
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
+using System.Management.Automation.Language;
using Microsoft.PowerShell.Commands.Internal;
namespace Microsoft.PowerShell.Commands
@@ -34,6 +36,13 @@ namespace Microsoft.PowerShell.Commands
}
}
+ ///
+ /// Gets or sets the delimiters to use when splitting the clipboard content.
+ ///
+ [Parameter]
+ [ArgumentCompleter(typeof(DelimiterCompleter))]
+ public string[] Delimiter { get; set; } = [Environment.NewLine];
+
private bool _raw;
///
@@ -68,11 +77,40 @@ namespace Microsoft.PowerShell.Commands
}
else
{
- string[] splitSymbol = { Environment.NewLine };
- result.AddRange(textContent.Split(splitSymbol, StringSplitOptions.None));
+ result.AddRange(textContent.Split(Delimiter, StringSplitOptions.None));
}
return result;
}
}
+
+ ///
+ /// Provides argument completion for the Delimiter parameter.
+ ///
+ public sealed class DelimiterCompleter : IArgumentCompleter
+ {
+ ///
+ /// Provides argument completion for the Delimiter parameter.
+ ///
+ /// The name of the command that is being completed.
+ /// The name of the parameter that is being completed.
+ /// The input text to filter the results by.
+ /// The ast of the command that triggered the completion.
+ /// The parameters bound to the command.
+ /// Completion results.
+ public IEnumerable CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
+ {
+ wordToComplete ??= string.Empty;
+ var pattern = new WildcardPattern(wordToComplete + '*', WildcardOptions.IgnoreCase);
+ if (pattern.IsMatch("CRLF") || pattern.IsMatch("Windows"))
+ {
+ yield return new CompletionResult("\"`r`n\"", "CRLF", CompletionResultType.ParameterValue, "Windows (CRLF)");
+ }
+
+ if (pattern.IsMatch("LF") || pattern.IsMatch("Unix") || pattern.IsMatch("Linux"))
+ {
+ yield return new CompletionResult("\"`n\"", "LF", CompletionResultType.ParameterValue, "UNIX (LF)");
+ }
+ }
+ }
}
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1
index 7cd4f492bf..62f8d77a44 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1
@@ -36,6 +36,13 @@ Describe 'Clipboard cmdlet tests' -Tag CI {
Get-Clipboard -Raw | Should -BeExactly "1$([Environment]::NewLine)2"
}
+ It 'Get-Clipboard -Delimiter should return items based on the delimiter' {
+ Set-Clipboard -Value "Line1`r`nLine2`nLine3"
+ $result = Get-Clipboard -Delimiter "`r`n", "`n"
+ $result.Count | Should -Be 3
+ $result | ForEach-Object -Process {$_.Length | Should -Be "LineX".Length}
+ }
+
It 'Set-Clipboard -Append will add text' {
'hello' | Set-Clipboard
'world' | Set-Clipboard -Append