Search code examples
powershelltextformatclipboard

Powershell - remove metadata from text


I want to write a Powershell script that, when run, will remove all formatting and similar metadata from the text in the clipboard.

I'm talking about stuff like copying some text from Microsoft Word and pasting it in Excel, normally this pastes it with bold, italic or etc. formatting the text had in Word but I want to paste it, as if I had copied it to notepad and copied it from there.

  1. I'd prefer to escape the need to emulate opening notepad.exe, pasting there and copying again, as I hope there is a more elegant/intelligent option.
  2. I know there is a copy option "Text only" in Office apps but not only does it not always work as you'd expect/want but copying into other applications doesn't have that option.

I know how to get the text from the clipboard with "Get-Clipboard" and subsequently set it with "Set-Clipboard" but I have no idea WHERE the darn formatting information is stored.


Solution

  • tl;dr

    • Just calling Get-Clipboard should give you the desired plain-text representation.

    • Add -Raw if you want the text returned as a single, multi-line string rather than as an array of lines.


    Background information:

    • Applications that copy rich text-based formats to the clipboard, such as Word and Excel copying RTF and HTML, usually also copy a plain-text representation of the same content.

    • PowerShell's Get-Clipboard cmdlet retrieves the plain-text representation:

      • by default in Windows PowerShell:

      • invariably in PowerShell (Core) v7+, where Get-Clipboard supports only plain-text retrieval.

      • Separately, in both PowerShell editions, you can use the -Raw switch to request that multi-line text on the clipboard be returned as a single, multi-line string rather than an array of lines, which is the default.


    [1] To express the default behavior with explicit arguments:
    Get-Clipboard -Format Text -TextFormatType UnicodeText; the documentation doesn't specify if and how enumeration value Text differs.