Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell script pasting text neither into cmdexe nor powershell.exe

Introduction

Recently, I came to the conclusion that typing something with :

[System.Windows.Forms.SendKeys]::SendWait("XY")

takes a much longer time than using :

 Set-Clipboard -Value "XY"
 [System.Windows.Forms.SendKeys]::SendWait("^V") (CTRL+V)

to copy and paste it, so I tried implementing it into my bigger script.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The problem

I have a minor difficulty with it due to it not pasting the copied text into powershell.exe or cmd.exe.

Using the exact same script to paste a message in notepad works.

In my case, I’m trying to paste a command into cmd. Let’s say the command is "echo $text" with $text = "HELLO"

    $text = "HELLO"
    
start cmd.exe
      
sleep -Milliseconds 300
     Set-Clipboard -Value "echo $text"
         
sleep -Milliseconds 300
     [System.Windows.Forms.SendKeys]::SendWait("^V")
           
sleep -Milliseconds 300
      [System.Windows.Forms.SendKeys]::SendWait("{Enter}")

It doesn’t end up pasting "echo HELLO" like it should, but it sends "^V" in the command line, but as I stated earlier, it does paste "echo HELLO" in notepad just fine which confused me a bit.

If you know the answer or a different approach to this issue, I’d appreciate it if you shared it with me!

>Solution :

Based on comments by @Zett42, this is corrected by using ^v instead of ^V.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading