Find control using AutomationId

Oct 31, 2012 at 6:49 PM
Edited Oct 31, 2012 at 6:51 PM


I am trying to use the Get-UIAControl using AutomationId property, but I get an exception. The command is something like:

Get-UIAWindow 'MyWindow' | Get-UIAPane -AutomationId 111 | Get-UIAControl -AutomationId 'ChildWindow1'

This last command in the pipe crashes with: 'The given path's format is not supported.'

I also tried with Get-UIAChildWindow, with the same result (the control is a child window in a MDI application).

Does this has to do with the fact that AutomationId is a string, not a number here? Is it because the element is a window itself? Or is it for some other reason?



EDIT: Forgot to say - the command does seem to work up to some point. Because I can see a red border around the element I want, so I guess it does identify it correctly, but crashes after that (and the border dissapears).

Oct 31, 2012 at 8:20 PM
Edited Oct 31, 2012 at 10:08 PM

Are you sure that your child window is below the pane in the UI Automation tree? I'd recommend to use UIA Verify or UIAutomationSpy -> the HIerarchy tab to examine the Automation tree.

During August or September, the module has been rewritten to meet classic cmdlets: now it returns ALL the children or descendants, and some parts of PowerShell code may require editing.

Working with child windows are a bit dubious deal: technically, ControlType.Window is a typical control, on the other hand, all windows of the process are somewhere on the top of hierarchy or near to.

Try the following:

1. getting all windows of an application and filtering out all but one (run as Administrator):


Start-Process services.msc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name help | Invoke-UIAMenuItemClick; 
Get-UIAWindow -pn mmc | ?{ $_.Current.ClassName !like "*#3276*" } | Get-UIAMenuItem -Name *help*topics* | Invoke-UIAMenuItemClick -PassThru:$false


2. getting the child window as the doctor said:


Start-Process calc -PassThru | Get-UIAWindow | Get-UIAMenuItem -Name Help | Invoke-UIAMenuItemExpand | Get-UIAMenuItem -Name About* | Invoke-UIAMenuItemClick;
Get-UIAWindow -pn calc | Get-UIAChildWindow | Read-UIAControlName


Some windows are ready for standard actions (Get-UIAChildWindow), some aren't.


Numbers (111) as input for a string parameter is not a problem.

Red squares are gone by the time when cmdlet saves a screenshot on failure (it's relatively new change).

Oct 31, 2012 at 10:18 PM

Regarding the AutomationId property, Name, AutomationId and Class(Name) are brothers that are being used equally.

MS UI Automation accepts all the properties the AutomationElement class provides for building a query (up to four parameters for query, as I have read somewhere). The UIAutomation module uses control type every time (the Get-UIAControl without the -ControlType parameter is an exception), and other three properties depending on cmdlet parameters. All the rest (IsEnabled, IsOffscreen, etc) are not used in queries. They can be used for filtering via the -SearchCriteria parameter (it's an array of hashtables: @{Name="aaa";AutomaitonId="*bbb*"},@{isEnabled="true"},@{isOffscreen="false"},@{processid="1???"} ).

In case we have difficulty getting a window of several, you might try other properties, for example, AccessKey or FrameworkId (if it makes sense, of course).