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!