diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs
index 69cea8361a..49ab5d768f 100644
--- a/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs
+++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/SetClipboardCommand.cs
@@ -44,6 +44,13 @@ namespace Microsoft.PowerShell.Commands
[Parameter]
public SwitchParameter PassThru { get; set; }
+ ///
+ /// Gets or sets whether to use OSC52 escape sequence to set the clipboard of host instead of target.
+ ///
+ [Parameter]
+ [Alias("ToLocalhost")]
+ public SwitchParameter AsOSC52 { get; set; }
+
///
/// This method implements the BeginProcessing method for Set-Clipboard command.
///
@@ -129,8 +136,28 @@ namespace Microsoft.PowerShell.Commands
if (ShouldProcess(setClipboardShouldProcessTarget, "Set-Clipboard"))
{
- Clipboard.SetText(content.ToString());
+ SetClipboardContent(content.ToString());
}
}
+
+ ///
+ /// Set the clipboard content.
+ ///
+ /// The content to store into the clipboard.
+ private void SetClipboardContent(string content)
+ {
+ if (!AsOSC52)
+ {
+ Clipboard.SetText(content);
+ return;
+ }
+
+ var bytes = System.Text.Encoding.UTF8.GetBytes(content);
+ var encoded = System.Convert.ToBase64String(bytes);
+ var osc = $"\u001B]52;;{encoded}\u0007";
+
+ var message = new HostInformationMessage { Message = osc, NoNewLine = true };
+ WriteInformation(message, new string[] { "PSHOST" });
+ }
}
}