diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs index b53ffeb01b3..69cea8361a8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs @@ -17,6 +17,7 @@ namespace Microsoft.PowerShell.Commands /// [Cmdlet(VerbsCommon.Set, "Clipboard", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2109826")] [Alias("scb")] + [OutputType(typeof(string))] public class SetClipboardCommand : PSCmdlet { private readonly List _contentList = new(); @@ -37,6 +38,12 @@ public class SetClipboardCommand : PSCmdlet [Parameter] public SwitchParameter Append { get; set; } + /// + /// Gets or sets if the values sent down the pipeline. + /// + [Parameter] + public SwitchParameter PassThru { get; set; } + /// /// This method implements the BeginProcessing method for Set-Clipboard command. /// @@ -53,6 +60,11 @@ protected override void ProcessRecord() if (Value != null) { _contentList.AddRange(Value); + + if (PassThru) + { + WriteObject(Value); + } } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 index 8cfc07b8755..7cd4f492bfa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 @@ -51,5 +51,36 @@ Describe 'Clipboard cmdlet tests' -Tag CI { $text | Set-Clipboard Get-Clipboard -Raw | Should -BeNullOrEmpty } + + It 'Set-Clipboard should not return object' { + $result = 'hello' | Set-Clipboard + $result | Should -BeNullOrEmpty + } + + It 'Set-Clipboard -PassThru returns single object with -Append = ' -TestCases @( + @{ Append = $false } + @{ Append = $true } + ){ + param ($append) + + $params = @{ PassThru = $true; Append = $append } + + Set-Clipboard -Value 'world' + $result = 'hello' | Set-Clipboard @params + $result | Should -BeExactly 'hello' + } + + It 'Set-Clipboard -PassThru returns multiple objects with -Append = ' -TestCases @( + @{ Append = $false } + @{ Append = $true } + ){ + param ($append) + + $params = @{ PassThru = $true; Append = $append } + + Set-Clipboard -Value 'world' + $result = 'hello', 'world' | Set-Clipboard @params + $result | Should -BeExactly @('hello', 'world') + } } }