Typical ways to obtain access to windows and controls (please note that you must run PowerShell as Administrator if you are not a domain admin or the settings don't allow you to) are as follow:

Get a window by its process name:
Get-UIAWindow -ProcessName $processname | Get-UIAButton -Name $btnName | Invoke-UIAButtonClick;
Get-UIAWindow -pn $processname | Get-UIAButton -Name $btnName | Invoke-UIAButtonClick;
Get-UIAWindow -ProcessName $processname | Get-UIAMenuItem -Name File | Invoke-UIAMenuItemClick | Get-UIAMenuItem -Name Open... | Invoke-UIAMenuItemClick | Get-UIAChildWindow -Name 'Open File'

Get a window by its process's Id:
Get-UIAWindow -ProcessId (Get-Process -Name mmc).Id
Get-UIAWindow -pid (Get-Process -Name mmc).Id

Get a window from process:
Start-Process calc -PassThru | Get-UIAWindow
Get-UIAWindow -Process (Get-Process -Name mmc)
Get-UIAWindow -p (Get-Process -Name mmc)

Get a window by window title:
Get-UIAWindow -Name $windowTitle | Get-UIAChildWindow | Get-UIATextBox -AutomationID ServerBox | Set-UIATextBoxText -Text "text";

Get a window by window handle:
Get-UIAWindowFromHandle -Handle 123456

Get the active window (it's recommended to run your script in a minimized window):

There are two ways to access the window object to use elements' object model. The first is via explicit assigning the window object to a variable:
$wnd = Start-Process calc -PassThru | Get-UiaWindow
$wnd.Move(1000, 300);
and the second is via implicit assigning:
(Start-Process calc -PassThru | Get-UiaWindow).Move(1000, 300);
There are no IDE (at least, I don't know such) in the PowerShell world that are able to provide Intellisense over any braces, so that the latter way to use is for those who have already experienced with methods and properties that the object model provides with.

More information about the object model could be found at Object model.

Tags: ControlType.Window

Last edited Feb 8, 2014 at 9:53 AM by apetrovskiy, version 13


No comments yet.