Send Keystroke

May 18, 2012 at 9:50 PM


this may sound extremly nooby (which I exspect I am) but i can´t send a Keystroke ( Enter)

If i do:

Get-UIAWindow -Name 'CSPSPServer' | Get-UIATextBox | set-uiacontroltext -text "i want enter {ENTER} "

it set the Text with the Enter.

Anyone has an idea?


May 19, 2012 at 8:42 PM


The Set-UIAControlText cmdlet is not an equivalent to the SendKeys call.
What do you expect sending a text with the {Enter} sign? the text box will be given the text and the control with the focus, most likely a button, will react to Enter.
SendKeys code is shorter, but the code that follows is pretty more reliable.
1. Remove {Enter} from your string
2. Add something like (next line of code) Get-UIAButton -Name 'your default button' | Invoke-UIAButtonClick
Or activate a control that should be the next in your test sequence.

You usually don't need to use Get-UIAWindow each time, the current window is stored in the [UIAutomation.CurrentData]::CurrentWindow.

Hope it helps

May 22, 2012 at 12:57 PM


thanks for your reply.

With {ENTER} i exspect to send a button (in this case ENTER) to the selected Box.

My problem is that i cant use an UIAButtonClick because there is no button for that.

Here is  a screenshot which may help you to understand my problem:


Thanks a lot for your help!

May 22, 2012 at 6:58 PM

It's quite unusual to use a text box as a default control. :) Are you sure that if you put the cursor to the text box and press Enter, it does what you expected?

Okay, if you are not an addict of beautiful code, just use something like



for example, to be sure that the focus is on the text box exactly in time, you can use the following lines of code:

Get-UIATextBox -AutomaitonId 'your text box Id';


if your environment is not stable enough (the focus can be moved, or a some pop-up might appear), you can use this construction twice or thrice.

for example, with a shorter timeout


Get-UIATextBox -AutomaitonId 'your text box Id' -Seconds 2;


Get-UIATextBox -AutomaitonId 'your text box Id' -Seconds 2;



The Set-UIAControlText cmdlet uses the PostMessage (or SendMessage, I already forgot, since the code is proven as working) API call that doesn't understand escape sequences that SendKeys consumes. Theoretically, I could translate all the curly bracket constructions to virtual keys (VK), but the work doesn't seem to be very useful. Here, on CodePlex, I know the project, called TestApi (by Microsoft). There are good key pressing features (among many others), and I planned to write cmdlets upon this project. But I have not started yet. :) Maybe, it's time to start this May.


Hope it helps

May 28, 2012 at 1:41 PM

Thanks for all your replys!

I wish you good luck with the future of this project and new future features. :)


May 28, 2012 at 3:08 PM

I'm just wondering, whether you are lucky enough with this help or are seeking for a better solution?

If you need something, propose features, xk0ni, and you'll be possibly given with them if efforts and value of your wishes will meet possibilities to create the features...

Jun 3, 2012 at 2:46 PM
xinliu wrote:

I'm just wondering, whether you are lucky enough with this help or are seeking for a better solution?

If you need something, propose features, xk0ni, and you'll be possibly given with them if efforts and value of your wishes will meet possibilities to create the features...


yes, actual the current solution is not that nice... My Laptop doesn´t auto-shutdown the light because of the SendKeys.

If you could do Virtual-Keys (like wm_keydown) this would be EXTREMLY awesome for me!


And i got another question: While using "Get-UIADocumentRangeText" i get the whole Document (as exspected). Is it possible to just get the last line of it? I just found a semi-solution (outputting all into a txt and then just use [-1] to get last line.


I wish you good luck for the future of this project. And im sorry if it´s hard to understand me. English is not my motherlanguage

Jun 14, 2012 at 12:30 PM

Hi, in a desperate attempt to do some thing in Metro UI, I added original SendKeys to the Set-UIAControlKeys cmdlet. I simply had no time to do anything better. :( The name of the cmdlet and the name of its parameter also sound badly. Nonetheless, it works, somehow, and, probably, slightly better than when called from .NET because it's usually given a control of our interest.

This worked similarly to any cmdlets accepting an AutomationElement:

Get-UIA[ControlType] - .... | Set-UIAControlKeys -Text "bla-bla-bla{ENTER}"

The cmdlet requires input, even if it's the desktop object:

Get-UIADesktop | Set-UIAControlKeys -Text "^{ESC}"

Jun 26, 2012 at 8:48 PM


thanks for your efforts but somehow i can´t get it to work.... it is properly me Mistake.

Im using Version 0.7.12 .NET35 at the moment.


If i do now:

Get-UIAWindow -Processname 'CSPSPServer' | Get-UIATextBox | Set-UIAControlKeys -Text "bla-bla-bla{ENTER}"


It returns "True" in powershell and then outputs bla-bla-bla is not a CMDlet etc.


Thanks for your help

Jun 26, 2012 at 9:09 PM


what do you mean saying " then outputs bla-bla-bla is not a CMDlet etc"? Does it output to somewhere else? Or it outputs to the text box, but Enter is not put there?


How does the Set-UIAControlKeys work? In fact, this is the same SendKeys that we have in VBScript or C#, and it broadcasts the keys.

The following line of code

Start-Process calc -PassThru | Get-UIAWindow | Set-UIAControlKeys -Text "1{+}1{ENTER}"

does exactly the same as the following does

Start-Process calc -PassThru | Get-UIAWindow; [System.Windows.Forms.SendKeys]::SendWait("1{+}1{ENTER}");

This means that if your text box is in the focus, it gets the keys. Otherwise, you may run something like

Get-UIAWindow -Processname 'CSPSPServer' | Get-UIATextBox | Set-UIAFocus | ...


Get-UIAWindow -Processname 'CSPSPServer' | Set-UIAFocus | Get-UIATextBox | ...

There is also a new cmdlet, Invoke-UIAHotkey. It's a temporary cmdlet, I'm planning to rewrite it, however, this is and will be available in some way. 

It also broadcasts the keys.

For example, if you run the following in PowerShell console

Invoke-UIAHotKey -Enter
it reacts as if you'd pressed the Enter there.


I think that if cmdlets can't hit the text box, the SendKeys also can't? Maybe, what accepts the Enter key is not a text box?

Jun 27, 2012 at 8:50 AM

Hi xk0ni,

probably, I've reproduced what you've been complaining about. When you run the Set-UIAControlKeys -Text "some text" cmd in the foreground PowerShell console, the console accepts "some text" as a command and spits it out.

Run a PowerShell console instance and paste the following to there:

# to load System.Windows.Forms namespace
ipmo ....\UIAutomation.dll

# painting a form
$form = New-Object System.Windows.Forms.Form
$form.Text = "testform"
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Left = 20;
$textbox.Top = 50;
$textbox.Visible = $true;
$textbox.AcceptsReturn = $true;
$textbox.Add_KeyPress(([EventHandler]$handler = {param($src, $e) if ($e.KeyChar -eq 13){$form.Close();}}));

and press Enter to start the form.

Run another instance of PowrShell and paste the following to the command prompt:

Get-UIAWindow -n testform | Get-UIATextBox | Set-UIAFocus | Set-UIAControlKeys -Text "abc{ENTER}"

In my tests, the second code put abc to the textbox field and closed the form.

If you throw out the Set-UIAFocus cmdlet from the pipeline, the code will return something like: "The term 'abc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

Is it your case, xk0ni?


If you need a form to be foreground, there you have two ways to do it

1) use Set-UIAFocus (not every control likes it, though)

2) run PowerShell console minimized, for example, using a Windows short cut (that is in turn minimized) to a cmd file (assuming the the folder C:\1 exists):

start /wait /normal /min powershell.exe -sta -command " &{dir "c:\1" >> "c:\1\dir_from_cmd.txt";}"

I often ran scripts in the second way, putting in the curly brackets the full path to a script and returning the result using the exit command...

Jun 27, 2012 at 1:03 PM
Edited Jun 27, 2012 at 1:11 PM

Sorry, It was my mistake.