none
Execute Powershell command copy text to clipboard (incl. `r`n ') RRS feed

  • Question

  • 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 a backtick. 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 a palaceholder string, wrap the text in single quotes (literal) and then replace the placeholder string with `r`n in 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!

    Thursday, July 19, 2018 8:12 AM

Answers

  • Sorry, I'm still not 100% sure what you are trying to do. Try building your variables before setting them to the clipboard:

     powershell.exe -command {$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = '`r`n'; $str = $str -replace $car, $clr; Set-Clipboard $str}

    This output to the clipboard as "this is a test`r`n1234"

    Is that what you were expecting?

    • Marked as answer by onefish Thursday, July 19, 2018 8:49 AM
    Thursday, July 19, 2018 8:35 AM

All replies

  • Can you use something like this? Instead of using `n and such?

    Set-Clipboard -Value @"
    
    Text here
    
    Text also here
    
    Final text
    
    Okay, now this is the final text
    
    "@

    Thursday, July 19, 2018 8:16 AM
  • Hi I.T, I am passing the string as part of the execution command parameter (not sure if I got the terminology right there). it can only be one line, but the original text may contain carriage returns and they need to be included in what is written to the clipboard.
    Thursday, July 19, 2018 8:21 AM
  • Sorry, I'm still not 100% sure what you are trying to do. Try building your variables before setting them to the clipboard:

     powershell.exe -command {$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = '`r`n'; $str = $str -replace $car, $clr; Set-Clipboard $str}

    This output to the clipboard as "this is a test`r`n1234"

    Is that what you were expecting?

    • Marked as answer by onefish Thursday, July 19, 2018 8:49 AM
    Thursday, July 19, 2018 8:35 AM
  • Almost, I didn't realise copying and pasting the code had garbled the $clr variable. It is supposed to be $clr = "`r`n" which replaces the placeholder text in $Str with the line breaks. I tried editing but the forum won't let me, says flagged as spam.

    Your help is greatly appreciated. I had tried every combination of brackets and placement of Set-Clipboard I could think of but obviously I missed the one that works!

    powershell.exe -command {$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = "`r`n"; $str = $str -replace $car, $clr; Set-Clipboard $str}

    Output is now:

    this is a test
    1234


    • Edited by onefish Thursday, July 19, 2018 8:49 AM
    Thursday, July 19, 2018 8:48 AM
  • Oops, looks like I spoke too soon. It works when run it in the console but not when I send the event directly to powershell.exe

    Any ideas what might need to be altered?

    Thursday, July 19, 2018 9:25 AM
  • What do you mean "Send the event directly to powershell.exe"? how are you executing this?
    • Proposed as answer by jrv Thursday, July 19, 2018 10:58 AM
    Thursday, July 19, 2018 10:02 AM
  • I an sending an "open application" system event to powershell.exe from a database. Kind of like executing it from cmd but I don't have to worry about escaping ^ cmd special characters. So far I cannot figure out a way that it works without the cmdlet call immediately following -command. There must be some syntax powershell required for direct execution of a command?

    This is sort of the equivalent of:

    cmd.exe /c cmd /c "echo.this is a test"| clip


    • Edited by onefish Thursday, July 19, 2018 10:48 AM
    Thursday, July 19, 2018 10:18 AM
  • Here is how to send complex commands to the command line:

    $command = 'Set-Clipboard -Value @"
    
    Text here
    
    Text also here
    
    Final text
    
    Okay, now this is the final text
    
    "@'
    
    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    powershell -encodedCommand $encodedCommand

    You can also capture the encoded text and paste it into a batch file or into the Task Scheduler


    \_(ツ)_/


    • Edited by jrv Thursday, July 19, 2018 10:49 AM
    Thursday, July 19, 2018 10:48 AM
  • Thanks, but I don't believe that is doing what I am trying to achieve. I am sending a system "open" event to powershell.exe with the command to execute and the text to write to the clipboard all included as parameters of the open event. I am not encoding text and commands inside powershell to send out to the command line. Hope that makes sense.

    Thursday, July 19, 2018 10:57 AM
  • No.  Makes no sense.  What is an "open event"?

    \_(ツ)_/

    Thursday, July 19, 2018 10:58 AM
  • System telling the exe file to open and in this case, do something based on parameters included with the event.

    I'll try another way to explain. Open cmd and type the below code and hit enter.

    powershell.exe -command Set-Clipboard 'TEXT'

    You have just sent an open exe event to powershell and told it to execute the command set-clipboard with TEXT.

    This is what I am trying to achieve but where the original 'TEXT' is full of special characters and carriage returns and need to be the same when written to the clipboard.

    I can do it by escaping everything under the sun in the original text but a single quote ' can't be escaped. Escaping everything is also more resource intensive when 'single quoted text' doesn't need to be excaped (accept ' substituted by ' ').

    I am trying to find a better way like just using placeholder text in the string and replacing it with `r`n in PS. Anyway, I am just repeating my original post I think.

    Thursday, July 19, 2018 11:14 AM
  • Single quotes can be escaped by using a second single quote.

    'The is John''s computer'

    The same is true of double quotes.

    "Jobn said, ""This is my computer."""


    \_(ツ)_/

    Thursday, July 19, 2018 11:17 AM
  • Yes, but single quotes cannot be escaped in double quoted (expandable) text and single quoted (literal) text treats `r`n literally so loses all carriage returns. Do you see the dilemma?
    Thursday, July 19, 2018 11:23 AM
  • Also, I was unable to find a way to string 'single quoted text' + "double `r`n quoted" + 'text together' and have it work as a parameter. Perhaps there is a way, I am far from expert.
    Thursday, July 19, 2018 11:26 AM
  • You are still not being clear about what you are trying to do.

    Text in a variable is not quoted. The quoting tells PS how to handle the text as it assigns it to a variable.

    What parameters are you referring to?  There are all kinds of things called parameters. 


    \_(ツ)_/

    Thursday, July 19, 2018 11:29 AM
  • Yes, but single quotes cannot be escaped in double quoted (expandable) text and single quoted (literal) text treats `r`n literally so loses all carriage returns. Do you see the dilemma?

    Single quotes do not need to be escaped in a double quoted string.

    Without an exact example of what you are doing there is not way to guess at what you are doing wrong.


    \_(ツ)_/

    Thursday, July 19, 2018 11:38 AM
  • I'm sorry, not sure how much clearer I can be. Lets say I have the below original text:

    This
    is an
    Example
    !@#$%^&*()_+

    I want to send the text and a command directly to powershell.exe to write it to the windows clipboard. I can do this using:

    powershell.exe -command Set-Clipboard "This`r`nis` an`r`nExample`r`n`!`@`#`$`%`^`&`*`(`)`_`+"


    Even spaces have to be escaped but the above works and the output written to the clipboard is the same as the original text. However, chuck a single quote ' in there somewhere or take away the opening and closing double quotes and it does not work.

    Now use only single quotes like:

    powershell.exe -command Set-Clipboard 'This`r`nis an`r`nExample`r`n!@#$%^&*()_+'

    and it works in the sense that the text is written to the clipboard but it is literal:

    This`r`nis an`r`nExample`r`n!@#$%^&*()_+

    All on one line. It is good not to have to escape all those characters but `r`n is now interpreted literally and the output is not the same as the original text.

    That is why I was trying to use the single quote method, but substitute carriage returns with placeholder text and then -replace it with "`r`n" as part of the code passed to powershell.exe.

    Any clearer?

    Thursday, July 19, 2018 11:56 AM
  • You must quote the command.  If using a single quote it must be doubled.

    Here is a working example:

    powershell -command {Set-Clipboard ("This`r`nis an`r`nExample`r`n" + '!@#$%^&*()_''')}


    \_(ツ)_/

    Thursday, July 19, 2018 12:12 PM
  • Here is an example with pure double quotes and a single quote.

    powershell -command {Set-Clipboard "This`r`nis an`r`nExample`r`n!@#$%^&*()_'"}


    \_(ツ)_/

    Thursday, July 19, 2018 12:14 PM
  • I am making a little bit of headway with your suggested code. Since I found I have to have a cmdlet at the start, I changed it to Invoke-Command and it is now evaluating. But it is not adding `r`n, any ideas why?

    powershell.exe Invoke-Command {$str = 'this is--INSERT_CRLF_HERE--a test'; $car = '--INSERT_CRLF_HERE--'; $clr = "`r`n"; $str = $str -replace $car, $clr; Set-Clipboard $str}

    output=

    this isa test



    • Edited by onefish Thursday, July 19, 2018 12:24 PM
    Thursday, July 19, 2018 12:23 PM
  • What is the purpose of adding Invoke-Command.  It serves no purpose.   The command is syntactically  impossible.  What command are you invoking?

    You have to be able to describe the purpose without reference to how to code it. What is it that you are trying to accomplish.


    \_(ツ)_/


    • Edited by jrv Thursday, July 19, 2018 12:29 PM
    Thursday, July 19, 2018 12:29 PM
  • I have no idea but it is evaluating now (albeit without `r`n), whereas the below code only evaluated when run inside a powershell console (i.e. would not execute as parameter of the open event).

    powershell.exe -command {$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = "`r`n"; $str = $str -replace $car, $clr; Set-Clipboard $str}

    I believe I have to call a cmdlet for powershell.exe to treat the parameter as something to be executed. Invoke-Command is a cmdlet that runs commands on local and remote computers.

    If you know of a better way I am all ears.



    • Edited by onefish Thursday, July 19, 2018 12:37 PM
    Thursday, July 19, 2018 12:36 PM
  • What code is it that you are trying to execute?

    The following does exactly what it says it will do. The strings with line breaks is placed on the clipboard.

    XXXthis is a test
    1234


    \_(ツ)_/

    Thursday, July 19, 2018 12:56 PM
  • Open cmd and run this:

    powershell.exe -command {$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = "`r`n"; $str = $str -replace $car, $clr; Set-Clipboard $str}
    

    It fails to execute.

    Now run this:

    powershell.exe Invoke-Command {$str = 'this is--INSERT_CRLF_HERE--a test'; $car = '--INSERT_CRLF_HERE--'; $clr = "`r`n"; $str = $str -replace $car, $clr; Set-Clipboard $str}
    

    It at least executes, but isn't interpreting `r`n properly.

    Output = 

    this isa test

    Thursday, July 19, 2018 1:11 PM
  • You didn't say you were using CMD.  The command line is escaped different in CMD.


    \_(ツ)_/

    Thursday, July 19, 2018 1:20 PM
  • This is how to do it at a CMD prompt.

    powershell -command "$str = 'this is a test--INSERT_CLRF_HERE--1234'; $car = '--INSERT_CLRF_HERE--'; $clr = \"`r`n\"; $str = $str -replace $car, $clr; Set-Clipboard $str"


    \_(ツ)_/

    Thursday, July 19, 2018 1:23 PM
  • Why do you need PowerShell at all? Just pipe to clip.exe.

    -- Bill Stewart [Bill_Stewart]

    Thursday, July 19, 2018 1:44 PM
    Moderator
  • I am actually not using cmd but the additional backslashes in \"`r`n\" were the trick, thank you for persisting with me. It is now working as expected :-)
    Thursday, July 19, 2018 1:47 PM
  • Hi Bill, do you know were can I find examples of the syntax for that? I seem to recall that you have to implement a lot more management of the clipboard using it directly. 

    Thursday, July 19, 2018 2:02 PM
  • Hi Bill, do you know were can I find examples of the syntax for that? I seem to recall that you have to implement a lot more management of the clipboard using it directly. 

    clip /?

    The help is just like most commands.


    \_(ツ)_/

    Thursday, July 19, 2018 2:04 PM
  • I am actually not using cmd but the additional backslashes in \"`r`n\" were the trick, thank you for persisting with me. It is now working as expected :-)

    If you had given accurate information in the beginning the problem would have been solved quickly. Try asking a clear and complete technical question in one or two sentences.  Skip the technical decoration and long storyline.


    \_(ツ)_/

    Thursday, July 19, 2018 2:05 PM
  • I think I may have misunderstood. the clip/? refers to using |clip in cmd. I am not using cmd so I assumed it might be possible to call clip.exe directly with a parameter like:

    clip.exe clip-write "some text here"

    Although I don't know the commands or syntax (if it's even possible).

    Thursday, July 19, 2018 2:17 PM
  • It helps if you understand how the command line and piping works.

    All clip.exe does is copy its standard input (which is usually the output of another command) and write it to the Windows clipboard.

    This description is not helpful if you are not familiar with the concepts of standard input and output.

    The | character means "take the output from command on the left side of the | character, and use it as input for the command on the right side of the | character."

    Look at the help for clip /?. It gives this example:


    dir | clip

    The help is helpful and states that this "Places a copy of the current directory listing into the Windows clipboard." That's correct: the dir command writes some stuff to standard output (a directory listing). The | character means "ok, use that directory listing output as input for clip.exe." Then clip.exe takes that input (i.e., the directory listing) and copies it to the Windows clipboard.

    PowerShell takes the | (pipe) concept further and lets you pipe objects (not just text), but the basic principle is the same: Output from left side becomes input for right side.

    Understanding these principles should help answer your question.


    -- Bill Stewart [Bill_Stewart]



    Thursday, July 19, 2018 2:28 PM
    Moderator
  • I think I may have misunderstood. the clip/? refers to using |clip in cmd. I am not using cmd so I assumed it might be possible to call clip.exe directly with a parameter like:

    clip.exe clip-write "some text here"

    Although I don't know the commands or syntax (if it's even possible).

    clip /? works anywhere.  Did you even try it.


    \_(ツ)_/

    Thursday, July 19, 2018 2:29 PM
  • Of course I tried it, read it and realised you are referring to piping to the clipboard as in "|clip" at the end of a command in cmd. I know how to use this already (I even used this in one of my early examples in this thread, for goodness sake). As I have stated several times I am not using cmd. So piping to clip.exe does not add any functionality or options in my situation unless I can call it directly and not through cmd. That is what I thought Bill was saying, but AFTER reading clip/? I realised that is not what he was saying. Hence my misunderstanding.

    Anyway, thank you all for your help.

    Thursday, July 19, 2018 3:26 PM
  • "clip" does not require "CMD".  It will run the same as any executable.  It runs in PowerShell.

    Again - you are making references to something that you are not disclosing.  There is really no way to address your issues as long as you do not give accurate and complete information.


    \_(ツ)_/

    Thursday, July 19, 2018 3:34 PM
  • Clip is a Win32 console program that accepts standard input.

    Both powershell.exe and cmd.exe are Win32 console programs that can produce standard output.

    Clip.exe works the same in powershell.exe as it does in cmd.exe.

    This is easily demonstrated by doing some testing.


    -- Bill Stewart [Bill_Stewart]

    Thursday, July 19, 2018 4:04 PM
    Moderator
  • I'm sorry man, I have tried to explain everything to you in so many ways it's making my head hurt. I'm not "not disclosing" anything. Other than I also know I can use "|clip" in PS but it has far less functionality than the cmdlets Set-Clipboard and Get-Clipboard. You would probably still use it if you were catering for machines running Win7 and <PS5.

    Anyway, the problem I set out to resolve has been resolved, thank you and good night.


    • Edited by onefish Thursday, July 19, 2018 4:08 PM
    Thursday, July 19, 2018 4:07 PM