Author Posts

July 19, 2018 at 7:36 am

I am trying to execute a powershell command to copy text to the windows clipboard including carriage returns and ALL special characters. The command is sent directly to powershell.exe and not using the console or script file. I can execute the below command ok:

powershell.exe -command Set-Clipboard 'TEXT'

I was using double quotes around text, substituting carriage returns in original text with `r`n and escaping all other special characters with ` It worked up until I got to a single quote ' which I understand is used by powershell to mean a literal text string. Great, perhaps I can do this without escaping all special characters in the original text.

So I changed approaches and wrapped un-escaped text in single quotes (except substituting 1 single quote ' for 2 single quotes "). Of course `r`n within the single quoted text is interpreted literally so does not provide line breaks.

A user on another forum suggested -EncodedCommand but unfortunately I am unable to produce any version of base 64 encoded strings (to include in the command) which match the same string encoded via the PS console. So that is not going to work.

I have been attempting to simply substitute carriage returns in the original text with an obscure string, wrap the text in single quotes (literal) and then substitute the obscure string back to `r`n within PS. I have gotten the substitution to work in the console directly but cannot figure out how to actually send it as a command. Below is the base command I've been working with (tried many different forms though).

powershell.exe -command Set-Clipboard $Str = 'this is a test--INSERT_CRLF_HERE--1234'; $Car = '--INSERT_CRLF_HERE--'; $clr = "`r`n"; $Str = $Str -replace $Car, $clr

Can the above command be modified to work? Is it possible to achieve the intended outcome without writing to a temp file? It is preferable to be able to use single quoted text as it is more robust and lightweight than trying to escape everything (even spaces) in the original text.

Thank you!

July 19, 2018 at 1:38 pm

Set-Clipboard -Value "This is a test`r`nof the emergency`r`nbroadcast system"

Output:

This is a test
of the emergency
broadcast system

July 19, 2018 at 10:24 pm

Sorry, re-read, need to upgrade my coffee. Try using a string format:

$str = 'This is a test {0}of the emergency {0}broadcast system' -f [System.Environment]::NewLine

Set-Clipboard -Value $str

July 20, 2018 at 12:21 am

Thanks so much for your help fellas, sorry for not getting back sooner. I posted this question twice and it totally disappeared from the forum both times so I gave up and posted it in another forum. Several hours later, after getting a working solution, I got a notice that this thread was active. When I came here to say thanks and post the solution the internet dropped out. 1/2hr later still out and it was the wee hours of the morning so I went to bed.

Below is the solution I came up with (with a bit of help).

powershell.exe -command "$str = 'this is--CRLF_P.HOLDER--a test'; $car = '--CRLF_P.HOLDER--'; $clr = \"`r`n\"; $str = $str -replace $car, $clr; Set-Clipboard $str"

Rob, I like your approach, it is cleaner than my current solution, but unfortunately it fails if the original text contains any curly brackets {}. I attempted to escape them but escaping with a backtick ` doesn't work inside 'single quoted text'. Do you know of a way to handle this?

—EDIT—

I got it working with {} in the original text by using the placeholder text and -replace. I like it better than my original solution as curly brackets are just not common in text strings, whereby CLRF is and Rob's solution handles CLRF more elegantly. However, is there yet a better way?

powershell.exe -command "$str = 'this is{0}a test{0}!@#$%^&*()--L_CURLY_BRACKET----R_CURLY_BRACKET--_+[]|\;'':",./?' -f [System.Environment]::NewLine; $lbko = '--L_CURLY_BRACKET--'; $rbko = '--R_CURLY_BRACKET--'; $lbk = '{' ; $rbk = '}' ; $str = ( $str -replace $lbko , $lbk ) -replace $rbko , $rbk ; Set-Clipboard $str"

Output:

this is
a test
!@#$%^&*(){}_+[]|\;':,./?

–EDIT (again)–

I just did some reading up on what -f does and answered my own question. Below is tidier but does require 2x passes of the original text to substitute { and } with {1} and {2}, otherwise I would get {1{2}. Works well though, thank you Rob!

powershell.exe -command "$str = 'this is{0}a test{0}!@#$%^&*(){1}{2}_+[]|\;'':",./?' -f [System.Environment]::NewLine,'{','}' ; Set-Clipboard $str"

July 21, 2018 at 4:40 am

IIRC prior to the introduction of the Set-clipboard cmdlet I was always annoyed that when I pipe text to clip.exe it always had a carriage return at the end. In this case it's a feature not a bug 😉