Search code examples
powershelltextcommand-lineclipboard

Get-Clipboard of Powershell : '-Raw' option?


From microsoft document manual, I found an option for 'Get-Clipboard' of PowerShell.
The option is '-Raw' :

-Raw : Gets the entire contents of the clipboard. Multiline text is returned as a single multiline string rather than an array of strings.

But I couldn't find any difference for adding '-Raw.
For example,

powershell.exe "Get-Clipboard -Format text " > MyText.txt

and

powershell.exe "Get-Clipboard -Format text -Raw" > MyText.txt

gives the same results. (Regardless of the content of the clipboard) Can you give me an example such that the use of '-Raw' option makes something different ?


Solution

  • $tempfile = New-TemporaryFile
    
    @'
    some 
    different lines 
    of text
    '@ | Set-Content $tempfile
    
    Get-Content $tempfile | Set-Clipboard
    
    $text = Get-Clipboard
    
    $text.count
    
    3
    
    $text = Get-Clipboard -Raw
    
    $text.Count
    
    1
    

    As it said it is a single string, rather than an array of strings.

    Here is one example of the many times it matters.

    $text = Get-Clipboard
    
    $text -match 'different'
    
    different lines
    
    $text = Get-Clipboard -Raw
    
    $text -match 'different'
    
    true