Home > Mobile >  Powershell script pasting text neither into cmdexe nor powershell.exe
Powershell script pasting text neither into cmdexe nor powershell.exe

Time:06-10

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.

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!

CodePudding user response:

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

  • Related