Set Clipboard - PowerShell


Overview

The Set-Clipboard command sets the text, HTML, or image data to the system clipboard. This enables quick transfer of data between different applications or commands.

Syntax

Set-Clipboard [-Value] <object> [-Format] <ClipboardFormat> [-Clear]

Options/Flags

  • -Value (): Specifies the object to store in the clipboard. Can be a string, HTML, or image data.
  • -Format (): Specifies the format of the data to be stored in the clipboard. Possible values include:
    • Text (default)
    • Html
    • Image

  • -Clear: Clears the current clipboard content before setting new data.

Examples

Simple Usage:

Set-Clipboard "Hello World"

Setting HTML Data:

$html = "<p>This is a paragraph in HTML.</p>"
Set-Clipboard -Value $html -Format Html

Setting Image Data:

$image = Get-Content -Path "path/to/image.png" -AsByteStream
Set-Clipboard -Value $image -Format Image

Clearing Clipboard:

Set-Clipboard -Clear

Common Issues

  • Cannot set non-string values: Ensure you specify the -Format option when setting non-string data.
  • Clipboard not updated: Check if the application has access to the clipboard and that the clipboard is not locked.
  • Incorrect data format: Verify that the data being set matches the specified clipboard format, e.g., HTML for -Format Html.

Integration

  • With Get-Clipboard: Retrieve the current clipboard content.
$clipboardContent = Get-Clipboard
  • In Scripts: Automate tasks like copying text to the clipboard for use in other commands.
$text = "Example text"
Set-Clipboard -Value $text
  • Get-Clipboard: Retrieves the current clipboard content.
  • Clear-Clipboard: Clears the clipboard.
  • PowerShell Extensions for Clipboard: Offers additional clipboard manipulation capabilities.