1

I'm completely new to PowerShell and am confused about some of its command syntax. I've been looking around google; however, some of the syntax I've found such as

Send-Keys "%(ea)Testing{Enter}{F5}"

has errors that I can't seem to get rid of.

Say I open Calculator with

var script = @" 
                Start-Process calc.exe
            ";

powerShell.AddScript(script);          
powerShell.Invoke();

How does one go about entering in values for fields and sending keystrokes? That is, enter in a 5, hit the - key, enter in a 6, then hit the ENTER key?

Or even better, how does one, using PowerShell, enter in some stock symbol (which isn't the default textfield) then search for it by hitting enter? (after opening firefox.exe, navigating to www.yahoo.com)

Thank you for your time

1
  • What are you trying to do with the web page? show it or parse it? Commented May 23, 2012 at 20:37

2 Answers 2

3
Key                  SendKeys
BACKSPACE            {BACKSPACE}, {BS}, or {BKSP}   
BREAK                {BREAK}   
CAPS LOCK            {CAPSLOCK}   
DEL or DELETE        {DELETE} or {DEL}   
DOWN ARROW           {DOWN}   
END                  {END}   
ENTER                {ENTER} or ~   
ESC                  {ESC}   
HELP                 {HELP}   
HOME                 {HOME}   
INS or INSERT        {INSERT} or {INS}   
LEFT ARROW           {LEFT}   
NUM LOCK             {NUMLOCK}   
PAGE DOWN            {PGDN}   
PAGE UP              {PGUP}   
PRINT SCREEN         {PRTSC}   
RIGHT ARROW          {RIGHT}   
SCROLL LOCK          {SCROLLLOCK}   
TAB                  {TAB}   
UP ARROW             {UP}   
SHIFT                +                 
CONTROL              ^                 
ALT                  %                 
BACKSPACE            {BACKSPACE}, {BS}, or {BKSP}  

One thing to bear in mind is that it takes a while for the application to start-up, you could be sending your keys before calculator is ready for them.

Try something like:

add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

Calc

start-sleep -Milliseconds 500

[Microsoft.VisualBasic.Interaction]::AppActivate("Calc")
[System.Windows.Forms.SendKeys]::SendWait("1{ADD}1=")

This should get you a quote from google:

 $IE=new-object -com internetexplorer.application
 $IE.navigate2("https://www.google.co.uk/finance?client=ob&q=NASDAQ:MSFT")
 $IE.visible=$true
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thanks for the response, is there any way to guarantee that a key is sent only after the previous command has finished?
I doubt it - all you're doing is dumping keystrokes into a black box - I'd use a sleep if you're worried about collisions.
You have to use start-sleep commandlet to wait for the program to start. The example above waits only 500 miliseconds because calculator starts in no time. start-sleep 4 will wait 4 seconds.
1

I can't go to www.yahoo.com as it automatically redirects me to uk.yahoo.com.

I was able however to go to http://finance.yahoo.com and do exactly what you want.

Bit ugly and relies on the page layout but it seems to be working:

add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

& 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe' -url http://finance.yahoo.com

start-sleep 3

[System.Windows.Forms.SendKeys]::SendWait("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}FB{ENTER}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.