bigpan
bigpan

Reputation: 419

How to set clipboard's plain text and html part at the same time in Powershell

When I copy some rich text from browser, the plain text part and html part of clipboard is existing at the same time.

But when I try to set clipboard in Powershell, they could not be set at the same time.

For example, Set-Clipboard -Value "abc" will set the plain text part of clipboard (and flush the html part) so that using Get-Clipboard -TextFormatType html will get blank. While Get-Content "a.html" | Set-Clipboard -AsHtml will set the html part and flush the plain text part of clipboard, so that using Get-Clipboard will get nothing.

How could I set clipboard's plain text and html part at the same time in Powershell?

Upvotes: 1

Views: 1487

Answers (1)

bigpan
bigpan

Reputation: 419

Inspired by How to copy both - HTML and text to the clipboard?

The following powershell script works.

$data = New-Object System.Windows.Forms.DataObject
$data.SetData([System.Windows.Forms.DataFormats]::Html, $text)
$data.SetData([System.Windows.Forms.DataFormats]::Text, $html)
[System.Windows.Forms.Clipboard]::SetDataObject($data)

Upvotes: 3

Related Questions